2012-06-27 13 views
1

以下方法在viewController中。方法選擇器指向不同的對象

refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshLabels) userInfo:nil repeats:YES]; 

是否有可能在選擇器而不是當前對象中選擇appDelegate(或另一個類)中的方法?

,如:

AppDelegate *ad = (AppDelegate *) [[UIApplication sharedApplication] delegate]; 
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector([ad refreshLabels]) userInfo:nil repeats:YES]; 

謝謝!

回答

1

試試這個。

AppDelegate *ad = (AppDelegate *) [[UIApplication sharedApplication] delegate]; 
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:ad selector:@selector(refreshLabels) userInfo:nil repeats:YES]; 

在AppDelegate中,應該有一個聲明refreshLabels方法,

target 
The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated. 

aSelector 
The message to send to target when the timer fires. The selector must correspond to a method that returns void and takes a single argument. The timer passes itself as the argument to this method. 

在你的問題設置的「自我」作爲參數爲目標的代碼,但您要調用的方法是AppDelegate

+0

我不敢相信我不知道這一點......謝謝! – Linus

+0

歡迎您.. :) – janusbalatbat

相關問題