How to use Gesture Recognizers in IB (iPhone/iPad)?

The Crazy Chimp picture The Crazy Chimp · Dec 12, 2011 · Viewed 14k times · Source

I have just discovered the Gesture Recognizers in IB and I would like to make use of them. I've tried out the Tap Gesture Recognizer however it doesn't seem to work. Here is what I've done...

  1. Dragged and dropped the 'Tap Gesture Recognizer' into my view controller in IB.

  2. Added the following method to the .m file...

    - (IBAction)viewTapped:(UIGestureRecognizer *)sender
    {
        // Do some stuff
    }
    
  3. Declared the method in the .h file...

    - (IBAction)viewTapped:(UIGestureRecognizer *)sender;
    
  4. Finally I linked up the IBAction to the 'Tap Gesture Recognizer' in IB.

When I tried running it I had no success - Have I missed something out?

Answer

rob mayoff picture rob mayoff · Dec 14, 2011

Control-click on the gesture recognizer in the nib. Under the "Referencing Outlet Collections" section of the popup, does it show any connections? It needs to be connected to the gestureRecognizers collection of your view, like this:

connections inspector

If it's not connected, drag the recognizer onto the view that you want it to be attached to. You can drag it onto the view in the design area, or you can drag it onto the view's row in the outline on the left.