Sending arguments to gesture recognizer initialization selector?

Fitzy picture Fitzy · May 11, 2012 · Viewed 12.8k times · Source

In my program, I have a UITapGestureRecognizer which I have initialized with initWithTarget: action:. I have passed in a selector to call a method by the name of PlanetTapped: (UIImageView *)aPlanet. This calls the method fine, however I would like to know how to pass arguments into action: like you would with performSelector: withObject. Is this popssible? It would make sense to allow you to send arguments to the UIGestureRecognizer's selector. Any help is appreciated.

Answer

sergio picture sergio · May 11, 2012

The correct signature for the method to call would be:

-(void) PlanetTapped: (UIGestureRecognizer*)gestureRecognizer

then you could access the view that received the gesture by calling:

-(void) PlanetTapped: (UIGestureRecognizer*)gestureRecognizer {

    UIImageView* aPlanet = gestureRecognizer.view;
    ...
}

Indeed, this is what UIGestureRecognizer reference states:

A gesture recognizer has one or more target-action pairs associated with it. If there are multiple target-action pairs, they are discrete, and not cumulative. Recognition of a gesture results in the dispatch of an action message to a target for each of those pairs. The action methods invoked must conform to one of the following signatures:

  • (void)handleGesture;
  • (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;