objective-c - XCODE 传递一个方法作为参数

标签 objective-c xcode methods parameters

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

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

最佳答案

正如@Daniel 在评论中提到的,您可以使用 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];
} 

关于objective-c - XCODE 传递一个方法作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10880349/

相关文章:

objective-c - UIViewAnimation 在 Objective-C 中不起作用

java - 为什么我的接口(interface)没有 JLS 声明的方法?

iphone - 在 viewDidUnload 中调用 [self <>] 安全吗?

ios - 将数组字符串对象转换为 float

ios - 某些日期的 NSDateformatter 问题

ios - 如何正确重用 Objective c 中的单元格?

ios - Storyboard本地化不包括 iOS 中的单个文件

iphone - UITabBarController,有什么可能的方法可以通过水平滑动移动下一个 View Controller?

java - 设置 JTextField 宽度以匹配 JPanel 的宽度

java - Java中用多个子类实现父类方法