IPhone: NSTimer

Take a question ... I have a Timer

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

But when I switch to another screen of my application, I want to change the selector ... How to change the selector? I know that I can stop my timer and set a new one, but I'm not used to resetting the time left to operate. Thank...

+3
source share
3 answers

You can not. NSTimer takes information about its targeting in its instance methods and does not provide any properties for subsequent modification.

You will need to cancel this timer and create a new one for the new target.

+3
source

, . setSelector. NSTimer . , setSelector.

+1

You can call the general selector, which, depending on the page displayed, calls other methods:

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

and obviously your selectorDispatcher method will look something like this:

    - (void) selectorDispatcher{

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

I think this should work ... let me know!

+1
source

All Articles