与 scheduleTimerWithTimeInterval 添加时 NSTimer 不触发选择器 [英] NSTimer not firing selector when added with scheduledTimerWithTimeInterval
本文介绍了与 scheduleTimerWithTimeInterval 添加时 NSTimer 不触发选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的代码片段:
I have a code snippet like this:
m_timer = [NSTimer scheduledTimerWithTimeInterval:timeOutInSeconds
target:self
selector:@selector(activityIndicatorTimer:)
userInfo:nil
repeats:NO];
当我这样称呼它时,在给定的 timeOutInSeconds 之后选择器不会被触发.但是,如果我将其修改为如下所示,则选择器将被调用两次.
When I call it like so the selector is not fired after the given 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?
我正在使用 XCode 5.1,并在 7.1.1 iPhone 4S 上构建
I am using XCode 5.1, and building on 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];
});
这篇关于与 scheduleTimerWithTimeInterval 添加时 NSTimer 不触发选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文