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 回答