您好,我在 NSMutableArray 上的 performSelector 遇到了棘手的问题。我可能误解了 performSelector 的作用。基本上我有带有对象的 NSMutableArray(它们都是基于我的类的对象,它继承自 UIButton,但我猜这并不重要),我需要向数组中的所有对象发送消息。
在我的类的头文件中,我声明了由选择器调用的函数。它看起来像这样:
-(void)scaleChangedWithANumber:(NSNumber *)scale;
在此示例中,我的数组包含 3 个对象。如果我逐项调用它们,它就完美了:
NSNumber *tmpNumber=[NSNumber numberWithFloat:scale];
ButtonOfElement *tmpButton1=[elementsButtonContainer objectAtIndex:0];
[tmpButton1 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
ButtonOfElement *tmpButton2=[elementsButtonContainer objectAtIndex:1];
[tmpButton2 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
ButtonOfElement *tmpButton3=[elementsButtonContainer objectAtIndex:2];
[tmpButton3 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
但是,如果我尝试在数组上调用 performSelector(我需要向数组中的所有对象发送消息):
NSNumber *tmpNumber=[NSNumber numberWithFloat:scale];
[elementsButtonContainer performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
tmpNumber=nil;
我得到错误:
[__NSArrayM scaleChangedWithANumber:]: unrecognized selector sent to instance 0x1737f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM scaleChangedWithANumber:]: unrecognized selector sent to instance 0x1737f0'
*** First throw call stack:
(0x34e456c3 0x370be97f 0x34e49227 0x34e47951 0x34d9f468 0x31d1 0x369a70eb 0x369a7447 0x369324d9 0x368f9c0d 0x36ae5649 0x3681df4f 0x3681c9b3 0x3682791f 0x368274eb 0x36814f29 0x36814843 0x337425d3 0x33742203 0x34e1a593 0x34e1a537 0x34e193b9 0x34d8c39d 0x34d8c229 0x3374131b 0x368688f9 0x2363 0x2308)
libc++abi.dylib: terminate called throwing an exception
我当然可以使用循环并逐项调用执行选择器,但我认为在数组上使用 performSelector 会更加优雅和清晰。