无法为发送到实例的无法识别的选择器添加目标,尽管方法在同一个类中 [英] Can't add target for UIButton - unrecognised selector sent to instance, despite method been in the very same class

查看:27
本文介绍了无法为发送到实例的无法识别的选择器添加目标,尽管方法在同一个类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为StickerClass的Objective-C iOS类中有一个UIButton。这个类有一个名为‘TheView’的UIButton的公共实例。在StickerClass的构造函数中,我使用:

设置了初始属性,如框架、层属性和子视图,以及点击时的目标
[theView addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];

有一个名为TopViewController的顶级类,它创建StickerClass的一个实例,然后使用对变量TheView(StickerClass实例中的UIButton)的公共访问,将按钮添加到TopView控制器(使用[self.view addSubview:[myInstance theView]])。

回到StickerClass,我有这个方法:

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

在标题中,我的签名就在@end:

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

然而,当我单击该按钮时,由于无法识别选择器,因此抛出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) 

有人知道为什么会发生这种情况吗?我想知道我是否设置了错误的目标,也就是说,因为它是self,所以它试图在TopView控制器中调用一个名为aMethod的方法,而不是在StickerClass中(并通过将该方法及其签名添加到具有类似NSLog的TopView控制器来测试这一点-它仍然引发了相同的错误)。任何非常感谢的想法。

谢谢!

推荐答案

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

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

并在创建StickerClass实例后立即在TopView控制器中调用此方法。

这篇关于无法为发送到实例的无法识别的选择器添加目标,尽管方法在同一个类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆