XCODE 传递一个方法作为参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10880349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:29:33  来源:igfitidea点击:

XCODE pass a method as parameter

objective-cxcodemethodsparameters

提问by 88fsantos

I wan't to pass a method as a parameter to another method, so it know the method to call when ends running. Is it possible?

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

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

回答by Vladimir

As @Daniel mentioned in comments you can use selectorfor that. Basic scheme will be the following:

正如@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];
}