iphone - 使用 NSTimer 关闭应用程序

标签 iphone cocoa nstimer

这里有点菜鸟。

我正在开发一个可以播放一些循环声音的应用程序。我想让用户能够使用计时器在一定时间后关闭应用程序。这个想法是用户按下一个按钮,一旦计时器耗尽,应用程序就会关闭。

此时,如果按下按钮,应用程序就会崩溃。

这是我到目前为止得到的:

- (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);

}

最佳答案

您需要正确定义计时器引用:

NSTimer *timer = [NSTimer scheduledTimerWithInterval: 10.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];

关于iphone - 使用 NSTimer 关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4412460/

相关文章:

objective-c - NSDateComponents - EXC_BAD_ACCESS 错误

ios - 如何用另一个 NSTimer 来停止 NSTimer?

ios - 如何控制uitextview中自动滚动的速度

multithreading - Swift:不断从服务器请求数据的最佳方式是什么

iphone - 如何检测 iPhone 的翻盖?

iphone - 如何从 iPhone 中的字符串设置 UIImageView 值?

ios - 如何将结构追加到数组中? swift 4

iphone - 两个日期之间的范围的 NSPredicate 没有按预期工作

xml - cocoa 中的 RSA 和 xml 格式的公钥

objective-c - Mac 应用程序的一般设计,基于文档还是?