指向不同对象的方法选择器



以下方法位于 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];

谢谢!

试试这个。

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

在您的应用程序委托中,应该有一个声明的刷新标签方法,

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"设置为目标的参数,但您尝试调用的方法位于应用程序委托中

最新更新