2011-06-17 64 views
3

有一個問題... 我有定時器iPhone:的NSTimer

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

但是當我移動到我的應用我想改變選擇的另一個屏幕......我怎樣才能改變選擇?我知道我可以停止我的計時器,並設置一個新的,但我不會重置一個時間仍然消防行動...謝謝....

回答

3

你不能。 NSTimer在其實例化方法中使用其定位信息,並且不公開任何屬性以稍後進行修改。

你將不得不使這個計時器無效並在新目標上創建一個新計時器。

1

它看起來像選擇器是不可變的。我會用setSelector方法將這個功能包裝到它的唯一小類中。在內部,使用私人選擇器創建NSTimer。在該方法中,調用使用setSelector方法設置的外部選擇器。

1

你可以稱之爲一個通用的選擇是,根據顯示在頁面上,調用其他方法:

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

和顯然比你的方法selectorDispatcher看起來像:

- (void) selectorDispatcher{ 

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

我覺得這應該工作...讓我知道!