简体   繁体   English

与ScheduledTimerWithTimeInterval添加时NSTimer不触发选择器

[英]NSTimer not firing selector when added with scheduledTimerWithTimeInterval

I have a code snippet like this: 我有一个这样的代码片段:

m_timer = [NSTimer scheduledTimerWithTimeInterval:timeOutInSeconds
                                                       target:self
                                                     selector:@selector(activityIndicatorTimer:)
                                                     userInfo:nil
                                                      repeats:NO];

When I call it like so the selector is not fired after the given timeOutInSeconds. 当我这样调用它时,选择器不会在给定的timeOutInSeconds之后触发。 However, if I modify it to be like the following, then the selector is called twice. 但是,如果我将其修改为如下所示,则选择器将被调用两次。

NSLog(@"Timer set");
m_timer = [NSTimer scheduledTimerWithTimeInterval:timeOutInSeconds
                                               target:self
                                             selector:@selector(activityIndicatorTimer:)
                                             userInfo:nil
                                              repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:m_timer forMode:NSRunLoopCommonModes];

Could anyone offer any suggestion as to what I am likely doing wrong? 有人可以对我可能做错的事情提出任何建议吗?

I am using XCode 5.1, and building on 7.1.1 iPhone 4S 我正在使用XCode 5.1,并在7.1.1 iPhone 4S上构建

Call this timer in main thread: 在主线程中调用此计时器:

dispatch_async(dispatch_get_main_queue(), ^{
   m_timer = [NSTimer scheduledTimerWithTimeInterval:timeOutInSeconds
                                                   target:self
                                                 selector:@selector(activityIndicatorTimer:)
                                                 userInfo:nil
                                                  repeats:NO];
});

You have 3 options in creating timer, as Apple states in the doc: 如Apple在文档中所述,您有3个创建计时器的选项:

  • Use the scheduledTimerWithTimeInterval:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode. 使用scheduledTimerWithTimeInterval:invocation:repeats:scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:类方法来创建计时器,并在默认模式下将其安排在当前运行循环中。
  • Use the timerWithTimeInterval:invocation:repeats : or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. 使用timerWithTimeInterval:invocation:repeatstimerWithTimeInterval:target:selector:userInfo:repeats:类方法可创建计时器对象,而无需在运行循环中进行调度。 (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.) (创建计时器后,必须通过调用相应的NSRunLoop对象的addTimer:forMode:方法将计时器手动添加到运行循环中。)
  • Allocate the timer and initialize it using the initWithFireDate:interval:target:selector:userInfo:repeats: method. 分配计时器并使用initWithFireDate:interval:target:selector:userInfo:repeats:方法对其进行初始化。 (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.) (创建计时器后,必须通过调用相应的NSRunLoop对象的addTimer:forMode:方法将计时器手动添加到运行循环中。)

The method you are using already schedules the timer on the current loop and you should not schedule another time. 您正在使用的方法已经在当前循环中安排了计时器,因此您不应安排其他时间。 In my opinion the problem is elsewhere, try (to make it easy) to put a fixed value instead of timeOutInSeconds . 我认为问题出在其他地方,请尝试(使其变得容易)放置固定值而不是timeOutInSeconds
Also the most common way to call something after a specific delay (that should not repeat) is use dispatch_after: 在特定的延迟后(不应重复),最常用的方法是使用dispatch_after:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //YOUR CODE
    });

Where the 2 is an arbitrary interval (in this case 2 seconds). 其中2是任意间隔(在这种情况下为2秒)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM