Swift Responder Chain (Zero Target in UIButton Target)

I have a problem using the responder chain in swift.

When I configure the target buttons with a null target, for example:

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

The action will send a chain of responders until the action is processed in the controller. So far so good :-)

But I want to intercept the action, execute some code and pass it to the controller. But I can’t find a way to do this quickly. In ObjC, this task is easy to do, I think there should also be a quick way.

Thanks in advance for any help :-)

+4
source share
2 answers

, .

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

.

, - .

+2

. MyContainerViewController . , , . .

class SomeChildViewController: UIViewController {
    @IBAction func closeAndShowSomething(sender: Any?) {}
        let showSelector = #selector(MyContainerViewController.showSomething(_:))
        let viewController: Any? = next?.target(forAction: showSelector, withSender: nil)
        dismiss(animated: true) {
            UIApplication.shared.sendAction(showSelector, to: viewController, from: self, for: nil)
        }
    }
}
0

Source: https://habr.com/ru/post/1584169/


All Articles