Parameters and selectors



In the first part of the chapter we implement the following method

and we make this an action for our gesture recognizer like so

The gesture recognizer is apparently able to be passed as a parameter for the action, how is this possible using selectors? Thanks.


Think back to when you learned what (id) was. How you’re used to seeing methods for selectors is like this.

But what is an (id)? It is a placeholder pointer for any type of object. So you could replace it with (UIGestureRecognizer*) if you want. This way you can take advantage of the specific class features of UIGestureRecognizer while having Xcode monitor errors in your code.

For example you can use:

-(void)doubleTap:(UIGestureRecognizer*)sender {
  if (sender.state == UIGestureRecognizerStateBegan) {
    //Do this in the selector to only execute if that is the state you want it to execute in

Similarly, you are giving up the dynamic nature of (id) when you use a specific class in a selector. Each has their purpose, but for this–the reason is so that you don’t execute the same actions twice. (One for Began and one for Ended.)