Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Responder Chain and Events

I'm a little confused. There are two UIViewControllers in UINavagationController's stack. I try to call the method of first UIViewController within the second UIViewController. See code below

class VC1: UIViewController {

    @objc func method1() {
        //not called after tap button
    }
}

class VC2: UIViewController {

    let button = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        button.addTarget(nil, action: #selector(VC1.method1), for: .touchUpInside)
    }
}

But method1 isn't called when I tap on the button. What's the problem?

like image 541
Andrey Maksimkin Avatar asked Aug 31 '25 10:08

Andrey Maksimkin


2 Answers

The responder chain is based on superviews and parent view controllers. So VC1 is not up the responder chain from VC2. The responder chain from VC2 leads to its parent (the UINavigationController); it does not go by way of any other children of the UINavigationController.

like image 159
matt Avatar answered Sep 03 '25 00:09

matt


The responder chain, starting from button, goes through button's ancestor views and view controllers. Since VC1 and VC2 are separate items in the navigation controller's stack, VC1 is not an ancestor of VC2. They are more like siblings of each other. So VC1 is not in button's responder chain.

You could just set VC1 as the target of the button action:

let vc1: VC1 = ...
button.addTarget(vc1, action: #selector(VC1.method1), for: .touchUpInside)

But that means VC2 needs reference to the existing VC1 instance. How do you get that reference? It depends on how you created VC2 and pushed it on the navigation stack.

If VC1 creates VC2 in code, then VC1 can just give the new VC2 a reference to self (the VC1) when creating the VC2.

If you have a storyboard segue from VC1 that pushes VC2 onto the stack, then you need to implement prepare(for: UIStoryboardSegue, sender:) in VC1, and in that method you need to hand self (VC1) to the destination of the segue (VC2).

like image 38
rob mayoff Avatar answered Sep 03 '25 00:09

rob mayoff