2011-10-04 42 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