当应用程序运行时,每隔60秒发生一次方法

时间:2009-09-27 14:45:56

标签: objective-c cocoa

我想在我的应用程序运行时每隔60秒运行一次我的方法,我该怎么做?

2 个答案:

答案 0 :(得分:8)

NSTimer

- (void) startTimer
{
  self.myTimer = [NSTimer scheduledTimerWithTimeInterval:60
                                                  target:self
                                                selector:@selector(timerFired:)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void) stopTimer
{
    [self.myTimer invalidate];
}

- (void) timerFired:(NSTimer*)theTimer
{
    NSLog(@"yay");
}

答案 1 :(得分:0)

虽然答案有效,但您的问题仍然不完整。

为什么需要定期运行此方法?如果要每隔60秒轮询一次iCal任务,这不是最好的解决方案。您需要做的是观察CalCalender商店发布的notifications