inessential by Brent Simmons

UIGestureRecognizer and nil-targeted Actions

I like the Cocoa responder chain — I like being able to specify nil as target and have the action message follow the responder chain until it’s handled (or not).

I like this so much I almost never specify a target — I (just about) always use nil.

But this doesn’t work with UIGestureRecognizer. The documentation states that “nil is not a valid value” for the target in addTarget:action:.

If I forget and use nil anyway, nothing happens. (Which is frustrating, because I don’t know why until I remember or look it up. Which just happened.)

How I work around it:

I specify the target in the same file where the gesture recognizer is created. I have it call a method something like this:

rs_performSelectorViaResponderChain looks like this:

I don’t like having to do this, but it works.

Why oh why

But my question is: why does UIGestureRecognizer not take nil as a target and work like everything else works?

I don’t have a theory.

Update 10:30 pm: Chris Parker writes on Twitter:

“They’re intended for specific gestures on views, rather than a hierarchy of actions. More of a virtual control.”

Update June 23, 2012 12:00 pm: Ben Lings asked on Twitter why I didn’t use -[UIApplication sendAction:to:from:forEvent:] instead of rs_performSelectorViaResponderChain.

Excellent question. I think I should be using sendAction:to:from:forEvent:. So I changed my code.