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];

ありがとう!

4

1 に答える 1

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.

質問のコードはターゲットの引数として「self」を設定していますが、呼び出そうとしているメソッドはAppDelegateにあります

于 2012-06-27T09:56:52.510 に答える