Search code examples
objective-cios6uitapgesturerecognizer

tap gesture recognizer won't target-action


I added a tap gesture recognizer into a UICollectionView but when I try to target action with control drag into the implementation of the controller the tap gesture recognizer will not add a method. Any ideas as to what may be happening?


Solution

  • Both of the following must be true:

    • You must ensure that you've specified the subclass in Interface Builder (e.g. if putting this method in your view controller, make sure you have the view controller's subclass specified for your scene in Interface Builder);

      enter image description here

    • If linking the action to an existing, the method must be in the implementation of the class for your must have a "return type" of IBAction (it behaves like a void, but you must specify IBAction or else IB won't let you link up the action), e.g.:

      - (IBAction)handleTap:(UITapGestureRecognizer *)gesture
      {
          // do whatever I want to handle the tap here
      }
      

      If a method has void or some other return type specified, Interface Builder will not let you to establish the connection.

    Personally, I develop the UI elements in Interface Builder first, and then control drag to the @interface and let Interface Builder actually add both the interface and shell of an implementation for me, and then add my code to that.