无法添加目标UIButton – 无法识别的select发送到实例,尽pipe方法在同一个类

我有一个名为StickerClass的Objective-C iOS类中的UIButton。 该类有一个名为“theView”的UIButton的公共实例。 在StickerClass的构造函数中,我设置了初始属性,如框架,图层属性和子视图,以及它被点击的目标,使用:

[theView addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside]; 

有一个名为TopViewController的顶级类,它创build了一个StickerClass实例,然后使用公共访问variablestheView(在一个StickerClass实例中的UIButton),将该button添加到TopViewController中(使用[self.view addSubview:[myInstance theView]] )。

回到StickerClass,我有这个方法:

 - (void)aMethod:(UIButton*)button { NSLog(@"Do some stuff..."); } 

在标题中,我在@end之前签名:

 - (void)aMethod:(UIButton*)button; 

但是当我点击button时,由于select器无法识别,会引发SIGABRT错误。 错误是:

 2014-09-06 21:13:23.448 Shy[6523:60b] -[UIControlTargetAction aMethod:]: unrecognized selector sent to instance 0x10922ca70 2014-09-06 21:13:23.450 Shy[6523:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControlTargetAction aMethod:]: unrecognized selector sent to instance 0x10922ca70' *** First throw call stack: ( 0 CoreFoundation 0x0000000101958495 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001016b799e objc_exception_throw + 43 2 CoreFoundation 0x00000001019e965d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x0000000101949d8d ___forwarding___ + 973 4 CoreFoundation 0x0000000101949938 _CF_forwarding_prep_0 + 120 5 UIKit 0x0000000100265f06 -[UIApplication sendAction:to:from:forEvent:] + 80 6 UIKit 0x0000000100265eb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17 7 UIKit 0x0000000100342880 -[UIControl _sendActionsForEvents:withEvent:] + 203 8 UIKit 0x0000000100341dc0 -[UIControl touchesEnded:withEvent:] + 530 9 UIKit 0x00000001005896f7 _UIGestureRecognizerUpdate + 5149 10 UIKit 0x000000010029ca15 -[UIWindow _sendGesturesForEvent:] + 928 11 UIKit 0x000000010029d6d4 -[UIWindow sendEvent:] + 909 12 UIKit 0x000000010027529a -[UIApplication sendEvent:] + 211 13 UIKit 0x0000000100262aed _UIApplicationHandleEventQueue + 9579 14 CoreFoundation 0x00000001018e7d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 15 CoreFoundation 0x00000001018e75f2 __CFRunLoopDoSources0 + 242 16 CoreFoundation 0x000000010190346f __CFRunLoopRun + 767 17 CoreFoundation 0x0000000101902d83 CFRunLoopRunSpecific + 467 18 GraphicsServices 0x0000000103acff04 GSEventRunModal + 161 19 UIKit 0x0000000100264e33 UIApplicationMain + 1010 20 Shy 0x0000000100001c73 main + 115 21 libdyld.dylib 0x0000000101ff05fd start + 1 22 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 

有谁知道为什么会发生这种情况? 我想知道是否我错误地设置了目标,也就是因为它是自我尝试在TopViewController中调用一个名为aMethod而不是在StickerClass中的方法(并且通过添加方法testing了这个方法,并且使用类似NSLog的方式对TopViewController进行了签名,仍然抛出相同的错误)。 任何想法非常赞赏。

谢谢!

该错误似乎源于使用addTarget:action:forControlEvents:在构造函数中。 如何创build一个单独的方法将目标添加到StickerClass实例,

 -(void)addButtonTarget { [self.theView addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside]; } 

并在刚刚创buildStickerClass的实例之后在您的TopViewController中调用此方法。