简体   繁体   English

iPhone:NSTimer

[英]iPhone: NSTimer

Have a question... I have Timer 有问题......我有计时器

 [NSTimer scheduledTimerWithTimeInterval:120                                                              
target:self                                           
selector:@selector(action1:)                                                      
userInfo:nil                                                       
repeats:YES];

But when I move to another screen of my app I want to change selector...How can I change selector ? 但当我移动到我的应用程序的另一个屏幕时,我想更改选择器...如何更改选择器? I know that I can stop my timer and set a new one, but I don't wont to reset a time remained to fire action...Thanks.... 我知道我可以停止我的计时器并设置一个新的计时器,但我不会重置一个剩余的时间来开火...谢谢......

You can't. 你不能。 NSTimer takes its targeting information in its instantiation methods, and doesn't expose any properties to modify that later. NSTimer在其实例化方法中获取其目标信息,并且不会公开任何属性以便稍后修改。

You're going to have to invalidate this timer and create a new one on the new target. 您将不得不使此计时器无效并在新目标上创建一个新计时器。

It looks like the selector is immutable. 看起来选择器是不可变的。 I would wrap this functionality into it's only tiny class with a setSelector method. 我将这个功能包装成它只有一个带有setSelector方法的小类。 Internally, create the NSTimer with a private selector. 在内部,使用私有选择器创建NSTimer Inside that method, call the external selector that's been set using the setSelector method. 在该方法内部,调用使用setSelector方法设置的外部选择器。

You might call a generic selector that, depending on the page shown, calls other methods: 您可以调用一个通用选择器,根据显示的页面调用其他方法:

    [NSTimer scheduledTimerWithTimeInterval:120                                                              
    target:self                                           
    selector:@selector(selectorDispatcher)                                                      
    userInfo:nil                                                       
    repeats:YES];

and than obviously your method selectorDispatcher will look something like: 而且显然你的方法selectorDispatcher看起来像:

    - (void) selectorDispatcher{

         if(pageshown1)
            [self callmethod1];
         else
            [self callmethod2];
    }

I think this should work...let me know! 我认为这应该有效......让我知道!

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

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