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.
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.
#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.
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?
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?
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!
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.