4

我不想将一个方法作为参数传递给另一个方法,所以它知道结束运行时要调用的方法。是否可以?

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

1 回答 1

15

正如@Daniel 在评论中提到的,您可以为此使用选择器。基本方案如下:

// 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];
} 
于 2012-06-04T11:23:53.210 回答