ios - 如何创建一个 "array of selectors"

标签 ios objective-c nsarray selector

我正在使用 iPhone SDK (3.0),我正在尝试创建一个选择器数组来调用一个类中的各种方法。

显然,我做错了(我认为 @selector 不被视为一个类,因此将它们填充到 NSArray 中不起作用)。

我试过这个,但显然是错误的。

有没有一种简单的方法来拥有这样的选择器数组?还是有更好的方法来遍历一组方法?

selectors = [NSArray arrayWithObjects:
                          @selector(method1),
                          @selector(method2),
                          @selector(method3),
                          @selector(method4),
                          @selector(method5),
                          @selector(method6),
                          @selector(method7), nil];

for (int i = 0; i < [selectors count]; i++) {
    if ([self performSelector:[selectors objectAtIndex:i]]) // do stuff;
}

最佳答案

你能存储字符串并使用 NSSelectorFromString 吗?

来自 docs

NSSelectorFromString

返回具有给定名称的选择器。

SEL NSSelectorFromString (
   NSString *aSelectorName
);

关于ios - 如何创建一个 "array of selectors",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2226520/

相关文章:

ios - 在 ios 的 UIWebview 中调用 javaScript

html - 除非调用此代码两次,否则 iOS UIWebView 标题不会更新

ios - 从不同线程使用时 NSDateFormatter 崩溃

iphone - 语义问题 : Incompatible pointer to integer conversion sending 'NSUInteger *' (aka 'unsigned int *' ) to parameter of type 'NSUInteger'

ios - NSArray 到 NSString ....发生一些崩溃

ios - 如果我不需要持久存储,那么以类似数据库的方式存储数据的最佳做法是什么? NSMutableArray?

ios - 如何 "invalidate"MFMessageComposeViewController (Obj-C)

ios - 在 iOS 中的导航栏正下方显示标签

ios - 是否可以编辑 UIAlertAction 标题字体大小和样式?

iphone - 检测 2 个 subview 的触摸