Change destination of Navigation Back button

前端 未结 4 1001
小蘑菇
小蘑菇 2021-01-12 03:14

How can I change the view controller the default navigation back button takes me to? The back button usually takes you back to the previous view controller. But what if I wa

4条回答
  •  离开以前
    2021-01-12 03:39

    This may just repeat what has been said above, but I solved this by modifying viewDidLoad in the last VewController. "theControllers" is an array of type UIViewController. In my case, I just wanted to go back to the root view controller, so the array just contains the root viewcontroller (first) and the current viewcontroller (last). Then "setViewControllers" replaces the current stack with the one I created.

    override func viewDidLoad() {
        super.viewDidLoad()
        var theControllers = [UIViewController]()
        theControllers.append(self.navigationController!.viewControllers.first!)
        theControllers.append(self.navigationController!.viewControllers.last!)
        self.navigationController?.setViewControllers(theControllers, animated: false)
    }
    

提交回复
热议问题