Share
Embed
Target-Action and the Responder Chain on iOS
Slides to accompany talk from NSManchester, 1st February 2016. With examples in Swift!
Example project at https://github.com/sammeadley/responder-chain
Sam Meadley
February 01, 2016
Transcript
27 slides
-
Target-Action and the Responder Chain on iOS with examples in
Swift! Sam Meadley @sammeadley -
Overview • Classic UIControl Target-Action • Gesture Recognizer Target-Action •
Interface Builder support -
Why does this matter? • Design (weak coupling) • Safety
(strong typing) • Performance (reduces introspection) -
UIControl Target-Action The Players: • Principal UIApplication class • UIApplication’s
keyWindow (UIWindow) • UIControl • The Responder Chain -
UIControl Target-Action • Principal UIApplication class pops top event from
queue and dispatches for handling via sendEvent(_:) Event A Event B Event C Event D UIApplication sendEvent(_:) -
UIControl Target-Action • Principal UIApplication class pops top event from
queue and dispatches for handling via sendEvent(_:) Event B Event C Event D • Forwards the event to the keyWindow, invoking the sendEvent(_:) method of the UIWindow object Event A UIWindow sendEvent(_:) UIApplication sendEvent(_:) -
UIControl Target-Action • UIWindow object delivers the event to the
view where the touch occurred Event A Event B Event C Event D Event A UIControl hitTest(_:withEvent:) UIApplication sendEvent(_:) UIWindow sendEvent(_:) -
UIControl Target-Action • UIWindow object delivers the event to the
view where the touch occurred Event A Event B Event C Event D UIApplication sendEvent(_:) Event A UIWindow sendEvent(_:) UIControl hitTest(_:withEvent:) • If the View is a UIControl, UIKit checks the target and action properties target: AnyObject, action: Selector -
UIControl Target-Action • The action message is dispatched via UIApplication's
sendAction(_:to:from:forEvent:) • The message is forwarded to the control contained in the target property Event B Event C Event D UIApplication sendAction(_:to:from:forEvent:) UIControl doSomething(_:) target: UIControl, action: doSomething -
UIControl Target-Action • Warning: If the selector is not implemented
on the target class, an NSInvalidArgumentException "unrecognized selector sent to instance" is thrown. Event B Event C Event D UIControl target: UIControl, action: doSomething UIApplication sendAction(_:to:from:forEvent:) -
UIControl Target-Action • Alternatively, passing a nil target hands off
the event to the Responder Chain. UIControl target: nil, action: doSomething UIApplication sendAction(_:to:from:forEvent:) -
UIControl Target-Action • Alternatively, passing a nil target hands off
the event to the Responder Chain. UIControl target: nil, action: doSomething UIView UIViewController UIWindow AppDelegate • Starting at the First Responder the chain is traversed until the specified action message is encountered. UIApplication doSomething(_:) UIApplication sendAction(_:to:from:forEvent:) -
UIControl Target-Action • If no responder implements the action method,
the invocation fails gracefully. UIApplication sendAction:to:from:forEvent target: nil, action: doSomething UIControl UIView UIViewController UIWindow AppDelegate UIApplication -
Gesture Recognizer Target-Action • Still possible with UIApplication’s sendAction(_:to:from:forEvent:) let
application = UIApplication.sharedApplication() application.sendAction(“didReceiveTapGesture:", to: nil, // Send up the responder chain. from: self, // UIKit drops any action sent from UIGestureRecognizer forEvent: nil) -
Gesture Recognizer Target-Action • Fighting the API • Risks exposing
UIGestureRecognizer to access methods/properties let application = UIApplication.sharedApplication() application.sendAction(“didReceiveTapGesture:", to: nil, // Send up the responder chain. from: self, // UIKit drops any action sent from UIGestureRecognizer forEvent: nil) -
Interface Builder support • Make no mistake, IB loves Target-Action
• Support for: • Targeted Actions • Nil-targeted Actions • Typed sender parameters (sometimes) -
Summary • Nil-targeted actions rule, mostly. • Gesture recognizers are
weird. • Interface Builder supports you, sometimes. -
Acknowledgments • UIGestureRecognizer and nil-targeted Actions by Brent Simmons. http://inessential.com/2012/06/22/
uigesturerecognizer_and_nil-targeted_act • Cocoa Design Patterns by Erik M. Buck. Donald A. Yacktman. http://www.goodreads.com/book/show/ 3048265-cocoa-design-patterns • Apple documentation • Paul Goracke (@pgor)