Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a UINavigationController which works great. Each view controller has its own button that pops the stack back to its root which also works great. However, I'd like to also be able to pop the stack back to its root by pressing a button on the tab bar (which is obviously in an entirely different class outside of the navigation stack).

Therefore, I created a delegate in the tab bar class which finds the view controller at the top of the stack and calls the method in that view controller to pop the stack back to the root. I printed something to the console to verify that the delegate is set up correctly and it is. Everything works exactly as it should, except that pressing the tab bar doesn't pop the stack back to its root.

Thoughts?

This is the view controller at the top of a UINavigationController stack

class BlankViewController202: UIViewController, MainContainerViewControllerDelegate {

    // pop to root
    func popToRoot() {
        self.navigationController?.popToRootViewController(animated: true)
        print("success")
    }

}

When this function above is called from within the view controller (when the user presses the button on the view controller itself), it pops the stack. But when this same exact method is called by a delegate from the tab bar, it doesn't pop the stack (but it does print to console so I know its hooked up properly).

This is where the button resides in the tab bar that when pressed should pop the stack back to its root

protocol MainContainerViewControllerDelegate {
    func popToRoot()
}

class MainContainerViewController: UIViewController {

    func moveToTab3(sender: UIButton!) {
        // ...
        let banana = BlankViewController202()
        self.delegate = banana
        delegate?.popToRoot()
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

The problem is that BlankViewController202() makes a whole new, separate BlankViewController202 — it is not the particular BlankViewController202 that is already in the interface as part of the navigation controller interface. It is that BlankViewController202 you want to talk to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...