Using NSTimer to shutdown app
Bit of a noob here.
I am developing an app that plays some looped sounds. I would like to give the user the 开发者_运维知识库ability to shut down the app after a certain amount of time using a timer. The idea is the user presses a button and the app will shut down once the timer runs out.
At the moment if the button is pressed, the app crashes.
Here's what I got so far:
- (IBAction)timer:(id)sender{
timer = [NSTimer scheduledTimerWithInterval: 10.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];
}
-(void) targetMethod: (NSTimer*) theTimer {
NSLog(@"timer?");
exit(0);
}
You need to properly define your timer reference:
NSTimer *timer = [NSTimer scheduledTimerWithInterval: 10.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];
精彩评论