How can i start an NSTimer when a user clicks a button?
EDIT:i had the code working and everything, but i put the code to hide the button above the NSTimer code, and when i placed it below the NSTimer code it worked!
How can i start an NSTimer when a user clicks a button?
EDIT:i had the code working and everything, but i put the code to hide the button above the NSTimer code, and when i placed it below the NSTimer code it worked!
Schedule it in the IBAction of the button. Check NSTimer Class Reference to know how to use timers.
Hi,
The line of code to start a timer is (read the docs here) :
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:nil];
This will call the method timerFired:
after 1 second i.e.
- (void)timerFired:(NSTimer *)timer {
NSLog(@"timer : %@ has gone off", timer);
}
To get this to trigger from a button, you need this method in your .h file :
- (IBAction)buttonPressed:(id)sender;
and in your .m file, detect the button press like this :
- (IBAction)buttonPressed:(id)sender {
NSLog(@"Button's been pressed!");
}
Then, in interface builder connect the button's 'Toiuch Up Inside' action to this method.