1
votes

I have a strange issue with UIGestureRecognizer

I've create a class where i declare the gesture recognizer, and put self as a target



-(id)initWithTextView:(UITextView*)theTextView withDelegate:(id<WordSelectionDelegate>)theDelegate
{
    if (self = [super init])
    {
        delegate = theDelegate;
        textView = theTextView;
        // init long press gesture to detect pressing on text elements
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressFromSender:)];
        [textView addGestureRecognizer:longPressGesture];
    }
    return self;
}

But the trick is when i actually make a long press gesture i have next error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString handleLongPressFromSender:]: unrecognized selector sent to instance 0x76227b0'

Why does the messages to self goes to String???

1
There's nothing wrong with the above code. You have to broaden your search. Put a breakpoint in the handleLongPressFromSender and make sure it's getting called (if not, is the WordSelection object falling out of scope and getting released on you? or did you type the method name incorrectly? or does that method which only takes one parameter not have the right parameter type?). If it is getting called, then broaden the search again and take a look at your WordSelectionDelegate definition and make sure you don't have an issue there.Rob

1 Answers

2
votes

By the way, the problem is undoubtedly that the object that has the handleLongPressFromSender instance method (i.e. the object you're initializing with initWithTextView) is falling out of scope by the time the UILongPressGestureRecognizer is invoked. You need to check the the scope of that object.

For example, assuming that the name of this class was MyTextViewHandler, imagine you had a viewDidLoad for some view controller that had something like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // do a bunch of initialization

    MyTextViewHandler *textViewDelegate = [[MyTextViewHandler alloc] initWithTextView:self.textview withDelegate:self];
}

If you did something like that in ARC project, you'd get the crash you describe (because the textViewDelegate object was a local object of viewDidLoad and will fall out of scope at the end of that method). If you make this delegate handler class an instance variable (or property) of your view controller, then this problem goes away.