#selector() And The Responder Chain

2016 - 04 - 21

Posted by Dominik Hauser

With the new syntax for selectors in Swift 2.2 the approach I used in “Utilize the responder chain for target action” produces a warning. Let’s fix that.

Protocols for president

First we add a protocol:

@objc protocol DetailShowable {  
  	func showDetail()  
}  

Then we can add an extension to

Selector  

as described in this awesome post by Andyy Hope that looks like this:

private extension Selector { 
  static let showDetail = #selector(DetailShowable.showDetail) 
}  

Adding the action to the responder chain is then as easy as this:

button.addTarget(nil,  
                 action: .showDetail,  
                 forControlEvents: .TouchUpInside)  

Then some responder object in the responder chain needs to conform to the
DetailShowable
protocol.

You can find the code on github.

Table of Contents