Reputation: 309
`- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSInteger *pushUpCount;
}
`- (IBAction)imPressed:(id)sender {
NSInteger pushUpCount = pushUpCount + 1;
NSString *strPushUp = [NSString stringWithFormat:@"%d", pushUpCount];
NSLog(strPushUp);
}
No my problem is that it says that the pushUpCount
is not declared. So I was wondering how could I make this "public
", so that all of the of the functions or IBActions
can use this variable. I know what the problem is I don't know how to fix it.
CODE EXPLANATION
All I'm doing here is setting the variable to 0. Before the user does anything. Then each time the button is pressed it will add 1 to the existing number. then I will change the text of a NSTextField
to the number but I know how to do that.(or I think I do at least).
So my basic question is..... How can I reuse a variable in another function or IBAction
Thanks in advance.
Upvotes: 0
Views: 34
Reputation: 21808
Make this variable a member of your class. I.e. declare it inside @interface
section and assign it 0 inside viewDidLoad
just like this: pushUpCount = 0;
Don't use it as a pointer (i'm pretty sure it's not what you need). Declare it NSInteger pushUpCount;
instead of NSInteger *pushUpCount;
Inside imPressed
just increment it pushUpCount++;
In order to make sure you understand everything i'll explain it very simple:
Your @interface
section in YourViewController.h
file should contain declaration of the variable:
@interface YourViewController : UIViewController
{
NSInteger pushUpCount;
}
@end
Now your code looks like:
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
pushUpCount = 0;
}
- (IBAction)imPressed:(id)sender {
pushUpCount++;
NSString *strPushUp = [NSString stringWithFormat:@"%d", pushUpCount];
NSLog(strPushUp);
}
Upvotes: 2