代码之家  ›  专栏  ›  技术社区  ›  RonLugge

为什么iphonesdk NSTimer不调用我告诉它的函数呢?

  •  2
  • RonLugge  · 技术社区  · 14 年前

    if(!myTimer)
    {
        NSLog(@"Setting up the timer!");
        myTimer=[NSTimer timerWithTimeInterval:1 
                                        target:self
                                      selector:@selector(timerTicked)
                                      userInfo:nil 
                                       repeats:YES];
    }
    

    感谢NSLog函数,我知道设置计时器的代码正在关闭,但它没有调用函数:

    -(void)timerTicked:(NSTimer*)theTimer
    {
    //NSLOG that tells me that this function isn't being fired
    }
    

    1 回复  |  直到 14 年前
        1
  •  4
  •   sjagr    14 年前

    您的选择器名称缺少尾随冒号。应该是这样的

    selector:@selector(timerTicked:)
    

    --在提问者评论后添加

    如果仍然不起作用,请检查以确保将计时器添加到运行循环中

    [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
    

    http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/doc/uid/20000319-CHDECCEE

    请参阅文档的讨论部分,它讨论了如何将计时器添加到运行循环,并指向运行循环文档。