BartoNaz
BartoNaz

Reputation: 2913

UIGestureRecognizer in InterfaceBuilder target/action

I am setting the UIGestureRecognizer for iPhone App. I am doing it in InterfaceBuilder by dragging Long Press Gesture Recognizer from Objects library to the view which must accept gestures.

After I add a Gesture Recognizer in this way, it has Referencing Outlet Collection pointing to the view which should accept the gesture.

Then in ViewController I add an IBOutlet pointing to the GestureRecognizer

IBOutlet UILongPressGestureRecognizer *gestRec;

and corresponding action for the Gesture Recognizer

-(IBAction)longPress:(id)sender;

So after it Gesture Recognizer shows in Interface builder the outlet and the action named longPress: with target named ControlManager

But my program crashes when I make long press gesture in iPhoneSimulator. When I NSlog the description of my GestureRecognizer it shows the following output:

<UILongPressGestureRecognizer: 0x6859620; 
state = Possible; 
view = <UIView 0x6859e20>; 
target= <(action=longPress:, target=<ControlManager 0x6859af0>)>>

I think that problem is in last line and that it should look like this:

target=<ControlManager 0x6859af0>; 
action=longPress:

Am I doing something wrong?

Upvotes: 0

Views: 2154

Answers (1)

Damien Del Russo
Damien Del Russo

Reputation: 1048

Did you implement -(void)longPress:(UIGestureRecognizer)recognizer in your .m? (I am suggesting updating the id -> UIGestureRecognizer and sender -> recognizer; IBAction is typedef to void but you might make it explicit)

You definitely want to implement the selector, and this type of crash is very common when the method (if implemented at all) doesn't match the selector. Even missing the colon could cause a mismatch.

Also, have you set up the debugger to break when an exception is raised? If not, go to debugger (6th "tab" from left on left pane), click + to add a new one, "Add Exception Breakpoint", and keep defaults). This will push you into the debugger as soon as an exception is raised, which is usually a useful place to be in the stack.

Good luck,

Damien

Upvotes: 1

Related Questions