-2

Hy again, I am still new to objective c and Xcode. I am building a small app and need to use a variable in a Method. I don't know where to put it so I can use it in the Method.

I have a button which starts the whole process but the Method of this button needs a variable which only should be created once (because its a random number) and be saved so the "Button Method" can use it to compare it. Where do I place it so my variable stays the same while my Method can use it?

Thx

- (IBAction)guessButton:(id)sender { 
    NSLog(@"Answer = %i", answer); 
    NSLog(@"Button Pressed"); 
    float guess = [[self.guessTextField text] floatValue]; 
    NSLog(@"Guess = %f", guess); 
}
Naresh
  • 16,698
  • 6
  • 112
  • 113
Roman H
  • 251
  • 2
  • 10
  • You can use the variable of the class instance which your method belongs to. – NF Lee Jul 11 '18 at 08:46
  • Instead of generalities please show your code. – matt Jul 11 '18 at 08:46
  • Here is my code: - (IBAction)guessButton:(id)sender { NSLog(@"Answer = %i", answer); NSLog(@"Button Pressed"); float guess = [[self.guessTextField text] floatValue]; NSLog(@"Guess = %f", guess); } This Method should get this varaible-> answer = arc4random() % 100 + 1; – Roman H Jul 11 '18 at 08:47
  • Simply create variable in .h file or create above the viewDidLoad function. You can call variable like self.variableName – Naresh Jul 11 '18 at 08:49
  • Please don't add information in the comments. [Edit](https://stackoverflow.com/posts/51280883/edit) your question. – vadian Jul 11 '18 at 08:51

2 Answers2

0

Create in .h file like this....

#import <UIKit/UIKit.h>
@interface GoogleMapsViewController : UIViewController
@property int answer;//If it's int. Here mention your variable type(If it string @Property NSString * answer;)
@end

And call with self.answer

- (IBAction)guessButton:(id)sender { 
    NSLog(@"Answer = %i", self.answer); 
    NSLog(@"Button Pressed"); 
    float guess = [[self.guessTextField text] floatValue]; 
    NSLog(@"Guess = %f", guess); 
}

enter image description here

Naresh
  • 16,698
  • 6
  • 112
  • 113
  • Xcode tells me -> Type name requires a specifier or qualifier – Roman H Jul 11 '18 at 08:55
  • It all worked out!!! Thank you... the only problem now is that Xcode wants me to set a developer Team. It won't let me compile without, u know why? – Roman H Jul 11 '18 at 09:07
  • You need to click on left side top file name. This is your project name (Simply go to targets). Her you need to select your developer account in General, see in Signing and select Team – Naresh Jul 11 '18 at 09:11
  • You see this link. https://stackoverflow.com/questions/39524148/requires-a-development-team-select-a-development-team-in-the-project-editor-cod – Naresh Jul 11 '18 at 09:17
0

It is recommended to store private variables in implementation class. We can define variable in .m file like this

@interface ViewController (){
    NSString *stringTodefineAge;
}

@property (readonly, strong, nonatomic) ModelController *modelController;
@end

@implementation RootViewController
....
Himan Dhawan
  • 894
  • 5
  • 23