2010-12-10 144 views
0

这里是一个noob的位。使用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); 

} 
+0

你的代码看起来合理,假设定时器是保留财产我猜?接下来要做的是在[timer scheduledTimer ...]调用之前和之后放置NSLog消息,并在targetMethod中另一个NSLog: – MahatmaManic 2010-12-10 19:31:41

+0

另一个需要注意的地方是确保当您在头中声明IBAction时您声明它与在.m - (IBAction)计时器中声明它的方式相同:(id)发件人,并且它在xib中正确连线。如果你看到像“无法识别的选择器”这样的崩溃事件,并且/或者你没有看到这些日志消息被触发,那么你没有将它连接到正确的位置。 – MahatmaManic 2010-12-10 19:33:22

+2

Apple不会批准任何故意关闭它的应用程序。如果你打算这样做,不要指望App Store成为发布的途径。 – Tommy 2010-12-10 19:38:38

回答

1

您需要正确地定义你的计时器参考:

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

正如@jsumners所指出的那样,您在定时器声明开始时缺少'NSTimer *'。你需要指定类型和它是一个指针。 – 2010-12-10 20:26:29

+0

感谢您的回复。我可能应该添加我有在头文件中声明的定时器,例如:NSTimer * timer;我将检查代码以确保所有建议都以thr代码存在。 – redned 2010-12-11 13:18:19

+0

解决!我有NSTimer *计时器= [NSTimer scheduledTimerWithInterval:10.0目标:自我选择器:@selector(targetMethod :) userInfo:nil重复:是];而不是NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(targetMethod :) userInfo:nil repeatats:YES];我在时间间隔前忽略了时间。感谢大家的回复。 – redned 2010-12-11 14:43:51