Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Target-Action and the Responder Chain on iOS

Sam Meadley
February 01, 2016

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
Tweet

More Decks by Sam Meadley

Other Decks in Programming

Transcript

  1. Why does this matter? • Design (weak coupling) • Safety

    (strong typing) • Performance (reduces introspection)
  2. 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(_:)
  3. 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(_:)
  4. 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(_:)
  5. 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
  6. 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
  7. 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:)
  8. 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:)
  9. 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:)
  10. 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
  11. 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)
  12. 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)
  13. Interface Builder support • Make no mistake, IB loves Target-Action

    • Support for: • Targeted Actions • Nil-targeted Actions • Typed sender parameters (sometimes)
  14. Summary • Nil-targeted actions rule, mostly. • Gesture recognizers are

    weird. • Interface Builder supports you, sometimes.
  15. 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)