All*_*len 2 iphone background timer nstimer invalidation
当我的应用程序进入后台时,我正试图使计时器无效.当您点击启动计时器并位于TimerController.m文件中的按钮时,将调用计时器.以下是如何调用它.
mytimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];//Timer with interval of one second
[[NSRunLoop mainRunLoop] addTimer:mytimer forMode:NSDefaultRunLoopMode];
Run Code Online (Sandbox Code Playgroud)
现在,当应用程序进入后台时,我想使mytimer无效,所以我尝试将[mytimer invalidate]; 进入 - (void)applicationDidEnterBackground :( UIApplication*)应用程序委托的应用程序方法.但这不会起作用,因为它在代表中未声明.我想通过将TimerController.h包含在委托中,这可行,但它不会.
所以,我显然不知道我在这里做什么.你能帮我吗?当应用程序进入后台时,如何使得mytimer无效?
zou*_*oul 11
UIApplicationDidEnterBackgroundNotification当应用程序进入后台时,还会发布通知.您可以在控制器中订阅此通知并处理其中的转换:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(goBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
// and later:
- (void) goBackground {
[timer invalidate], timer = nil;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2962 次 |
| 最近记录: |