iosswiftuibuttonnsresponder

Responder Chain in Swift (nil target in UIButton target)


I have got a problem using the responder chain in swift.

When I setup a buttons target using a nil target like:

someButton.addTarget(nil, action:"addButtonTapped:", forControlEvents: .TouchUpInside)

The action will be send up the responder chain until the action is handled in the controller. So far so good :-)

But I want to intercept the action, execute some code and relay it on to the controller. But I cannot find a way to do this in swift. In ObjC this task is easy to do so I guess there should be a way in swift too.

Thanks in advance for any help :-)


Solution

  • One of my co-workers gave me the hint to recreate the selector and send it manually again.

    let selector = Selector("someButtonTapped:")
    let target: AnyObject? = self.nextResponder()?.targetForAction(selector, withSender: button)
    UIApplication.sharedApplication().sendAction(selector, to: target, from: self, forEvent: nil)
    

    This recreates the responder chain and relays the new message to the next responder.

    I hope that somebody will find this useful.