2012-06-04 30 views
4

我wan't傳遞一個方法作爲參數傳遞給另一種方法,因此它知道端部運行時調用的方法。可能嗎?XCODE傳遞一個方法作爲參數

[self bringJSON:(NSString *)_passedValua:(NSObject *)anotherMethod]; 
+0

您是否嘗試過使用選擇器? – Daniel

+0

AFAIK這是不可能 – Lefteris

回答

15

由於@丹尼爾在評論中提到,您可以使用selector。基本方案如下:

// Method declaration - method accept selector as parameter 
- (void) bringJSON:(NSString *)_passedValua toMethod:(SEL)anotherMethod]; 

// Method call - create selector from method name (mind the colon in selector - it is required if your method takes 1 parameter) 
[self bringJSON:jsonString toMethod:@selector(methodName:)]; 

// Implementation 
- (void) bringJSON:(NSString *)_passedValua toMethod:(SEL)anotherMethod]{ 
    ... 
    if ([target respondsToSelector:anotherMethod]) 
     [target performSelector:anotherMethod withObject:_passedValua]; 
} 
+0

要命!韓國社交協會很多弗拉基米爾和Daniel! – 88fsantos

+0

漂亮,尊貴......感謝名單的好友:) – abhi