Sam Heather
Sam Heather

Reputation: 1503

Can't add target for UIButton - unrecognised selector sent to instance, despite method been in the very same class

I've got a UIButton in an Objective-C iOS Class, called StickerClass. The class has a public instance of a UIButton called 'theView'. In the constructor of StickerClass, I set the initial properties such as frame, layer properties and subviews, and the target for when it's clicked on, using:

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

There's a top level class, called TopViewController, which creates an instance of StickerClass and then, using the public access to the variable theView (the UIButton in an instance of StickerClass), adds the button to the TopViewController (using [self.view addSubview:[myInstance theView]]).

Back in StickerClass, I have this method:

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

and in the header, I have the signature just before the @end:

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

and yet when I click the button, a SIGABRT error is thrown as the selector is unrecognised. The error is:

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) 

Does anybody have any idea why this is happening? I wondered if I was setting the target wrongly, i.e. because it was self it was trying to call a method in the TopViewController called aMethod rather than in StickerClass (and tested this by adding the method and it's signature to TopViewController with a similar NSLog - which still threw the same error). Any thoughts much appreciated.

Thanks!

Upvotes: 4

Views: 2126

Answers (1)

pbasdf
pbasdf

Reputation: 21536

The error seems to stem from using addTarget:action:forControlEvents: in the constructor. How about creating a separate method to add the target to the StickerClass instance,

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

and call this method in your TopViewController just after you create the instance of StickerClass.

Upvotes: 1

Related Questions