-1

Let's say that I want to create a GestureRecognizer object and have my whole logic in there. So when I add the NSObject as target for my recogniser:

initWithTarget:self action:@selector(doTheRecognizerThing:)

I'm facing an issue and I'm not sure why. I get an

unrecognized selector sent to instance

error although the doTheRecognizerThing method is properly implemented in my NSObject. If I add the target and implement the method in any other object such as UIView or UIViewController I don't get any issues. Is there an explanation for that?

EDIT

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self
                                                                                              action:@selector(doTheRecognizerThing:)];

Implemantation

- (void) doTheRecognizerThing:(UIPanGestureRecognizer *)panGestureRecognizer {
  //blah blah blah
}

Error Message:

-[CALayerArray doTheRecognizerThing:]: unrecognized selector sent to instance 0x14648a80
2015-01-06 23:05:20.788 Gestures[2203:1094298] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray doTheRecognizerThing:]: unrecognized selector sent to instance 0x14648a80'
alecnash
  • 1,750
  • 1
  • 18
  • 42
  • 1
    Can you share the code of your "doTheRecognizerThing:" implementation? The ':' makes me believe you should be passing a parameter which I don't see in your code example. – KirkSpaziani Jan 06 '15 at 21:58
  • Probably you implemented `doTheRecognizerThing`, not `doTheRecognizerThing:`. – Hot Licks Jan 06 '15 at 21:59
  • 1
    (An UIView or UIViewController is also an NSObject.) – Hot Licks Jan 06 '15 at 22:00
  • If you spell everything right it will work. – Hot Licks Jan 06 '15 at 22:01
  • 3
    Show us the code and complete error messages and stack trace. – Hot Licks Jan 06 '15 at 22:02
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. – Hot Licks Jan 06 '15 at 22:03
  • @HotLicks do you need more code? – alecnash Jan 06 '15 at 22:03
  • It would be good to see some context. And the *complete* error message and at least the first few lines of the stack trace. – Hot Licks Jan 06 '15 at 22:04
  • right when you create the object do an NSLog(@"%@", @([self respondsToSelector: @selector(doTheRecognizerThing:)]); If that writes out false 'self' doesn't actually implement the method. – KirkSpaziani Jan 06 '15 at 22:07
  • 1
    Ok So based on your code the target is a CALayerArray, are you adding this in a category? – KirkSpaziani Jan 06 '15 at 22:10
  • @KirkSpaziani it returns 1 – alecnash Jan 06 '15 at 22:10
  • @KirkSpaziani and no – alecnash Jan 06 '15 at 22:11
  • Are you sure you aren't calling setTarget: on the panGestureRecognizer at a later time? I honestly can't see what could be going wrong based on what we have so far. It looks like you're constructing the object with the right target, and all is well, but then the message is being sent to an Array... – KirkSpaziani Jan 06 '15 at 22:12
  • or addTarget: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/index.html#//apple_ref/occ/cl/UIGestureRecognizer – KirkSpaziani Jan 06 '15 at 22:13
  • @KirkSpaziani still the same – alecnash Jan 06 '15 at 22:16
  • Last thing I have. Put a breakpoint in your doTheRecognizerThing: - see if it gets hit before you crash. If it does then you have a second target on the gesturerecognizer. Use the debugger to try to get the list of objects that are the targets and try to figure out how they got there. Good luck! – KirkSpaziani Jan 06 '15 at 22:19
  • If your class in of NSObject then what is the purpose of UIPanGesture there ? Also in which class you allocated the PanGesture ? From your error message It seems like the target is deallocated. – Midhun MP Jan 06 '15 at 22:30
  • @MidhunMP in the NSObject class. If I allocate it in the View or ViewController class everything works fine but I don't get it. It should be the same on the NSObject – alecnash Jan 06 '15 at 22:35

2 Answers2

1

At one point you will add the recognizer to a view. This holds the recognizer strongly. At some point your object goes away, while the view stays – and with it the recognizer. That calls the method, but the object is gone and replaced by something else.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
-1

Try this:

initWithTarget:ObjectWithThatMethod action:@selector(doTheRecognizerThing:)

make sure doTheRecognizerThing is in header file. And make sure that NSObject is not nil.

CarlHere
  • 43
  • 1
  • 11