Iphone 如何在用户在应用程序中的整个时间内让NSTimer运行?

Iphone 如何在用户在应用程序中的整个时间内让NSTimer运行?,iphone,ios,background,nstimer,background-process,Iphone,Ios,Background,Nstimer,Background Process,如何在用户在应用程序中的整个时间内让NSTimer运行 我想能够改变视图控制器和执行不同的功能,而所有的定时器是在后台进行。如果计时器达到0,则我想触发一个事件。当应用程序处于后台时,我是否可以在应用程序内部设置计时器?甚至当iPhone的屏幕关闭时 谢谢你的回答 在Appdelegate中执行这段代码,使计时器成为其属性 self.timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer

如何在用户在应用程序中的整个时间内让NSTimer运行

我想能够改变视图控制器和执行不同的功能,而所有的定时器是在后台进行。如果计时器达到0,则我想触发一个事件。当应用程序处于后台时,我是否可以在应用程序内部设置计时器?甚至当iPhone的屏幕关闭时


谢谢你的回答

在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];

在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];

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

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

}

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

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

}

你可以在App delegate中使用该计时器你可以在App delegate中使用该计时器我应该何时调用此方法?-(void)applicationdFinishLaunching:(UIApplication*)application我应该何时调用此方法?-(void)applicationdFinishLaunching:(UIApplication*)application