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

时间:2012-06-27 09:24:03

标签: objective-c xcode object selector

以下方法在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 个答案:

答案 0 :(得分: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'设置为target的参数,但是你试图调用的方法是在AppDelegate中