5StringRyan
5StringRyan

Reputation: 3634

Issues using UITapGestureRecognizers in Interface Builder

I'm attempting to use the UITapGestureRecognizer object that can be found in Interface Builder. I've dragged a single "UITapGestureRecognizer" from the object library to a single view xib. I then create an IBAction method from this tap gesture, for a simple test, I'm just printing an "NSLog" message to the console once there is a tap on the view. I've run this, and the tap method isn't being called. I right click the view in IB and I noticed that there is a warning "!" on the view's "Outlet Collections" I see:

Outlet Collections
gestureRecognizers - Tap Gesture Recognizer (!)

The warning states: UIView does not have an outlet collection named gestureRecognizers.

What do I need to do to remedy this?

Upvotes: 12

Views: 6954

Answers (7)

Mr.Anonymous
Mr.Anonymous

Reputation: 820

I think you have not wired the UITapGestureRecognizer properly to your code.

When you drop a UITapGestureRecognizer on your xib Xcode automatically makes the necessary referencing outlet connections.

You only need to create an IBAction method in your code and then wire it to the selector of the UITapGestureRecognizer placed in xib.

I have attached screenshots for ur reference.

enter image description here

enter image description here

Hope this helps!!

Upvotes: 4

orkoden
orkoden

Reputation: 20026

I had forgotten to check that User Interaction Enabled has to be checked for the views the gesture recognizer is added.

Upvotes: 2

hammady
hammady

Reputation: 988

Mr.Anonymous solution is correct. No need to implement the delegate in the view controller or set it. However, you should check that User interaction enabled is checked (in the properties window on the right), especially if you are attaching the recognizer to a label.

Upvotes: 4

Lee Probert
Lee Probert

Reputation: 10859

The warning shouldn't be an issue. It is only there because you can add multiple gesture recognizers as an IBOutlet Collection but this isn't required.

Turn on zombies in your scheme to get better error messages whilst debugging. In my case the View controller I was trying to message with my gesture handler wasn't being instantiated in Interface Builder.

Also, it's not necessary to implement the delegate protocol and it will work on any UIView.

Upvotes: 0

Plasma
Plasma

Reputation: 2912

I did this to add a Double Tap Gesture to my Navigation bar, completely in code and found it very easy to use ....

In view did load ...

//Add double tap gesture to Navbar
UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
                                    initWithTarget:self action:@selector(navigationBarDoubleTap:)];
tapRecon.numberOfTapsRequired = 2;
[self.navigationController.navigationBar addGestureRecognizer:tapRecon];

I then have a method ...

#pragma mark - Auto Refresh Method
- (void)navigationBarDoubleTap:(UIGestureRecognizer*)recognizer {
      //Do Stuff Here
}

Maybe you could adapt this.

Plasma

Upvotes: 1

Kate Morris
Kate Morris

Reputation: 56

Two things to check: Does your view controller (the one that contains the UIView) implement the UIGestureRecognizerDelegate protocol?

Once it implements UIGestureRecognizerDelegate, make sure you've set the gesture recogniser's delegate property to the view controller. I used a storyboard to make the connection.

I do this and I don't get any errors (IOS 5.1, xCode 4.3).

Upvotes: 1

JamesB41
JamesB41

Reputation: 703

Take a look at this.

Specifically the screen shot of the outlet setup. Hopefully that will help.

Upvotes: 0

Related Questions