2013-05-03 72 views
0

我该如何让NSTimer在整个应用程序的使用过程中始终运行?如何在用户使用应用程序的整个过程中使用NSTimer?

我希望能够改变视图控制器,并执行不同的功能,同时还能定时器是怎么回事INT背景。如果计时器达到0,那么我想发起一个事件。当应用程序处于后台时,我是否也可以在应用程序内部安装计时器?或者即使iPhone的屏幕关闭了?

感谢您的答案!

+0

可以采取定时器在应用程序委托 – Impossible 2013-05-03 06:23:55

回答

1

运行此方法在应用程序委托

-(void) timerfunc{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(yourfunction) userInfo:nil repeats:YES ]; 
    [pool release]; 

} 
+0

当我应该调用此方法? – KKendall 2013-05-04 07:26:58

+1

- (void)applicationDidFinishLaunching:(UIApplication *)application – Dolo 2013-05-04 07:28:28

2

在Appdelegate中做这段代码使计时器成为它的属性。

self.timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer 
                target: self 
                selector: @selector(timerExpired:) 
                userInfo: nil 
                repeats: NO]; 

    //Run the timer on the runloop to ensure that it works when app is in background 
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode: NSDefaultRunLoopMode]; 
相关问题