Using iOS Declared ID in Buttons

I am very new to iOS development but I have been doing Java and C before.

I am trying to create a simple timer app and when the user hits "start" the button text turns to "reset" but the compiler throws me "Using undeclared id" btnStart ""

I took out the rest of the code because everything was working until I tried to change the button text.

I am sure it is correctly declared in the .h file and I think it might have to do with adding another @property argument to the button itself, but that doesn't work. How to declare a button correctly?

thank

my ViewController.m

- (IBAction)btnStart:(id)sender {

   [btnStart setTitle: @"RESET" forState: UIControlStateNormal]; //Error shown here
}

      

my ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *lblDisplay;

- (IBAction)btnStart:(id)sender;
- (IBAction)btnStop:(id)sender;

@end

      

+3


source to share


3 answers


Please declare the IBOutlet button in the .h or .m file where you specified -

@property (weak, nonatomic) IBOutlet UILabel *lblDisplay;

@property (weak, nonatomic) IBOutlet UIButton *btnStart;

      



and use it like this:

   [self.btnStart setTitle: @"RESET" forState: UIControlStateNormal]; 

      

+1


source


The sender must be the button itself. try it



- (IBAction)btnStart:(UIButton *)sender {
   [sender setTitle: @"RESET" forState: UIControlStateNormal];
}

      

+2


source


You can set button title for two states in viewDidLoad function like this

[btnStart setTitle: @"RESET" forState: UIControlStateNormal];
[btnStart setTitle: @"Start" forState: UIControlStateSelected];

      

and in your function use this code

- (IBAction)btnStart:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setSelected:!btn.isSelected];
}

      

0


source







All Articles