2011-10-04 98 views
1

如果我有一个在Foo类中定义的函数,并且我的类Bar有对Foo类的引用,我可以使用@selector调用该函数吗?类似于@selector在单独的类中的功能

@selector([Foo someFunctionInFoo:]) 

我只使用@selector来调用同一个类中的函数。我试着做类似于上面的代码的东西,但它不起作用,我不确定它是否可能。谢谢。

回答

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 
+0

我怎么会当我虽然创建一个UIBarButonItem配合这个选择?像self.ParentController.FilterButton = [[UIBarButtonItem alloc] initWithTitle:@“Filter”style:UIBarButtonItemStyleBordered target:self action:@selector(FilterButtonPressed :)]; – Crystal

+0

'target'参数指定将消息发送到哪个对象,'action'参数指定要使用哪个选择器。这就是所有您需要发送信息的信息。 –

+0

用'Foo'代替'self'。该方法基本上是在按下条形按钮时执行'[target performSelector:action]'。我已经更新了我的答案以包含此... – hypercrypt