0
`- (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.

1 Answer 1

2
  1. 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;

  2. 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;

  3. 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);
 }
Sign up to request clarification or add additional context in comments.

3 Comments

I don't really under stand what you mean but I changed It and I get the pointer(at least I think its the pointer of the variable) printed out in the NSLOG. I have updated my question could you explain it more.
What exactly you can't get?
never mind you updated your answer and it helped me a lot. I get what you did thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.