@selector到一个单独的类中的函数

时间:2011-10-04 15:26:38

标签: iphone

如果我有一个在类Foo中定义的函数,并且我的类Bar引用了类Foo,我可以使用@selector来调用该函数吗?像

这样的东西
@selector([Foo someFunctionInFoo:])

我只使用@selector来调用同一个类中的函数。我尝试过类似上面的代码,但它没有用,我不确定它是否可能。感谢。

1 个答案:

答案 0 :(得分:0)

@selector不存储任何调用信息,只存储选择器。

[Foo performSelector:@selector(someFunctionInFoo:) withObject:nil];

你也可以这样做:

Class class = SomeClass;
id obj = [class someObject];
SEL sel = @selector(someFunction);
[class performSelector:sel];
[obj performSelector:sel];

只要实现someFunction

要指定按钮等使用哪个目标,请将目标和操作设置为两个属性,例如:

[[UIBarButtonItem alloc] initWithTitle:@"Filter"
                                 style:UIBarButtonItemStyleBordered
                                target:foo // The object to send the message to
                                action:@selector(someFunctionInFoo:)]; // The message to send
相关问题