Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-22-2009, 12:45 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 16
monty747 is on a distinguished road
Default Question about how to use Objects

I'm sure I'm over explaining this-I'm not really sure how to properly ask it and, obviously, this is very new to me.

I have written an app that has 5 methods which have within them the same sections of code. Each method is a little different before and after the common sections so there have to be 5 different methods. The common sections contain many int variables that have values passed to them from the screen which are then run through a math formula to display a value to the screen.

My question is: How do I group these same sections into one and then reference them from each individual method? Said another way, I'm looking for a type of "subroutine" or "goto" function.

Am I thinking of object-oriented programming the right way? Does it work like this? I want to run my specific method code up to a point, then jump to the one common method and then come back and finish to the end of the individual method. I hate making a change to one and then having to change it in all 5.

Any URL's or basic help to get me started would be greatly appreciated.
monty747 is offline   Reply With Quote
Old 10-22-2009, 01:46 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Seattle, WA USA
Posts: 590
ethanwa is on a distinguished road
Default

Quote:
Originally Posted by monty747 View Post
I'm sure I'm over explaining this-I'm not really sure how to properly ask it and, obviously, this is very new to me.

I have written an app that has 5 methods which have within them the same sections of code. Each method is a little different before and after the common sections so there have to be 5 different methods. The common sections contain many int variables that have values passed to them from the screen which are then run through a math formula to display a value to the screen.

My question is: How do I group these same sections into one and then reference them from each individual method? Said another way, I'm looking for a type of "subroutine" or "goto" function.

Am I thinking of object-oriented programming the right way? Does it work like this? I want to run my specific method code up to a point, then jump to the one common method and then come back and finish to the end of the individual method. I hate making a change to one and then having to change it in all 5.

Any URL's or basic help to get me started would be greatly appreciated.
Here's an example of what you need to do:

myclass.h
Code:
#import ...

@interface ...
	
}

@property ... 

- (IBAction)button1;
- (IBAction)button2;
- (int)commonMethod:(int)aNumber;
- (void)commonLogMethod:(int)finalNumber;

@end
myclass.m
Code:
#import ...

@implementation ...

@synthesize ...

- (IBAction)button1 {
    int y = 7;
    y = y + [self commonMethod:6];
    [self commonLogMethod:y];
}

- (IBAction)button1 {
    int y = 8;
    y = y + [self commonMethod:7];
    [self commonLogMethod:y];
}

- (int)commonMethod:(int)aNumber {
   int x = 5;
   x = x + aNumber;
   return x;
}

- (void)commonLogMethod:(int)finalNumber {
   NSLog("%d", finalNumber);
}

Follow the code along from two different button touches and you'll see how to call common methods in your code.

Ethan
ethanwa is offline   Reply With Quote
Old 10-22-2009, 01:43 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 16
monty747 is on a distinguished road
Default

That's great. Thank you. It does have the flow of what I am looking for. However, it only passes one variable. What if I have 10 variables that need to be passed from button1 to commonMethod?
monty747 is offline   Reply With Quote
Old 10-22-2009, 01:47 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Seattle, WA USA
Posts: 590
ethanwa is on a distinguished road
Default

Quote:
Originally Posted by monty747 View Post
That's great. Thank you. It does have the flow of what I am looking for. However, it only passes one variable. What if I have 10 variables that need to be passed from button1 to commonMethod?
You would do something like this

.h
Code:
- (IBAction)button1;
- (void)commonMethod:(int)aNumber name:(NSString *)aName city:(NSString *)aCity;
.m
Code:
- (IBAction)button1
{
    [self commonMethod:1 name:@"Ethan" city:@"Seattle"];
}

- (void)commonMethod:(int)aNumber name:(NSString *)aName city:(NSString *)aCity 
{
     NSLog("%d, %@, %@", aNumber, aName, aCity);
}
Ethan
ethanwa is offline   Reply With Quote
Old 10-22-2009, 06:02 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 16
monty747 is on a distinguished road
Default

Seems like there should be an easier way but this totally works. It makes complete sense now that I see it. I'm already using it. Very helpful-thank you!
monty747 is offline   Reply With Quote
Old 10-22-2009, 06:05 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Seattle, WA USA
Posts: 590
ethanwa is on a distinguished road
Default

You could always put your int's in an array, and then just pass the array over. That would cut down on the amount of parameters you have in your method.

Code:
- (IBAction)button1
{
    	NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:4];
	[myArray addObject:@"test1"];
	[myArray addObject:@"test2"];
	[myArray addObject:txtField1.text];
	[myArray addObject:txtField2.text];
        
        [self commonMethod:myArray];

        [myArray release];
}

- (void)commonMethod:(NSMutableArray *)anArray 
{
     for (NSString *aString in anArray) 
	{
		NSLog(@"%@", aString);
	}
}

Last edited by ethanwa; 10-22-2009 at 06:14 PM.
ethanwa is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 465
13 members and 452 guests
Domele, Duncan C, Feldspar, MacBook MH, Objective Zero, patapple, peterwilli, pipposanta, PixelInteractive, Punkjumper, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:04 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0