【问题标题】:How do you pass a variable with the method name to @selector()?如何将带有方法名称的变量传递给@selector()?
【发布时间】:2012-03-07 23:55:40
【问题描述】:

我需要在循环中将方法名称传递给@selector(method),其中方法是包含在数组中的值。

[myButton addTarget:self action:@selector(method:)  forControlEvents:UIControlEventTouchDown];

如何在循环中定义@selector() 以避免对每个@selector(method) 进行硬编码?我可以传入一个字符串值吗?

【问题讨论】:

    标签: objective-c selector


    【解决方案1】:

    您可以使用NSSelectorFromString(),将NSString 映射到SEL,也可以使用sel_registerName(),将char* 映射到SEL

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多