NSTimer不触发选择器 [英] NSTimer not firing the selector

查看:78
本文介绍了NSTimer不触发选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有ARC的ios5.0中,在我的rootviewcontroller中,我在由应用程序委托持有的安全管理器对象中调用一个方法.在这种方法中,我将计时器设置如下

In ios5.0 with ARC, in my rootviewcontroller I call a method in a security manager object that is held by the app delegate. In that method I setup the timer as below

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self 
                                       selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 

但是,这永远不会触发选择器,即. updateModel:永远不会被调用.可能是什么问题?是否有另一种更有效的方式可以在不使用NStimer的情况下做到这一点?

However, this never fires the selector ie. updateModel: never gets called. What may be wrong? Is there another more efficient way I can do this without using NStimer?

推荐答案

您似乎与计时器变量有些混淆.

You seem to be a bit mixed up with your timer variable.

您初始化了一个新计时器,但实际上并没有使用它.您是要使用初始化的计时器还是ApplicationDelegate.timer?

You initialize a new timer but you aren't actually using it. Do you want to use the timer you initialized or do you want to you ApplicationDelegate.timer?

这是两种可能的解决方案.

Here are the two possible solutions.

选项一(假设您有一个名为ApplicationDelegate的类实例,并且它具有timer属性):

Option One (assuming that you have a class instance titled ApplicationDelegate and that it has a timer property):

ApplicationDelegate.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:ApplicationDelegate.timer forMode:NSRunLoopCommonModes];

选择二:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

这篇关于NSTimer不触发选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆