Reputation: 33
I'm a very new to iOS development but I have dabbled in Java and C before.
I'm trying to create a simple timer application and when the user presses 'start', the button text will turn into 'reset' but the compiler throws me a "Use of undeclared identifier 'btnStart'"
I've taken out the rest of the code because everything worked run until I tried to change the button text.
I'm pretty sure its correctly declared in the .h file and I'm thinking it might have to do with adding another @property argument for the button itself but that didn't really work. How do I properly declare a button then?
Thanks
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
Upvotes: 3
Views: 336
Reputation: 123
You can set title of the button 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];
}
Upvotes: 0
Reputation: 10479
The sender should be the button itself. Try this
- (IBAction)btnStart:(UIButton *)sender {
[sender setTitle: @"RESET" forState: UIControlStateNormal];
}
Upvotes: 2
Reputation: 399
Please declare button IBOutlet in .h or .m file where you have declared --
@property (weak, nonatomic) IBOutlet UILabel *lblDisplay;
@property (weak, nonatomic) IBOutlet UIButton *btnStart;
and use it like this
[self.btnStart setTitle: @"RESET" forState: UIControlStateNormal];
Upvotes: 1