Unable to add target for UIButton - unrecognized selector sent to the instance even though the method was in the same class

I have a UIButton in an iOS Objective-C class called StickerClass. The class has a public UIButton instance called "theView". In the StickerClass constructor, I set initial properties such as frame, layer properties and subviews, and the target for the click using:

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

      

Here's a top-level class called TopViewController that instantiates StickerClass and then, using public access to theView variable (UIButton on StickerClass instance), adds a button to the TopViewController (using [self.view addSubview:[myInstance theView]]

).

Back on StickerClass, I have this method:

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

      

and in the header I have a signature before @end:

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

      

and yet, when I click the button, a SIGABRT error occurs because the selector is not recognized. Mistake:

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 anyone know why this is happening? I was wondering if I am setting the target wrong, i.e. Because he himself tried to call a method on the TopViewController called aMethod and not on StickerClass (and tested it by adding the method and its signature to a TopViewController with a similar NSLog), which still throws the same error). Any thoughts are greatly appreciated.

Thank!

+3


source to share


1 answer


The error seems to be related to use addTarget:action:forControlEvents:

in the constructor. How to create 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 right after you instantiate StickerClass.

+1


source







All Articles