Change destination of Navigation Back button

前端 未结 4 1034
小蘑菇
小蘑菇 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:34

    Probably easiest way to achieve behaviour you want is to use navigationController.setViewControllers(controllers, animated: true).

    If you want to go 2 controllers back from controller A instead of only one, use this custom segue when presenting A:

    class ReplaceControllerSegue: UIStoryboardSegue {
        override func perform() {
            if let navigationController = sourceViewController.navigationController as UINavigationController? {
                var controllers = navigationController.viewControllers
                controllers.removeLast()
                controllers.append(destinationViewController)
                navigationController.setViewControllers(controllers, animated: true)
            }
        }
    }
    

    Here is nice tutorial how to set custom class for your segue in Interface Builder: http://blog.jimjh.com/a-short-tutorial-on-custom-storyboard-segues.html

    If you are presenting controller via code, use this category:

    extension UINavigationController {
        func replaceCurrentViewControllerWith(viewController: UIViewController, animated: Bool) {
            var controllers = viewControllers
            controllers.removeLast()
            controllers.append(viewController)
            setViewControllers(controllers, animated: animated)
        }
    }
    

    Then just use self.navigationController!.replaceCurrentViewControllerWith(newController, animated: true) instead of self.navigationControllerPushViewController(newController, animated: true).

    It's easy to adjust this code to remove as much controllers as you need.

提交回复
热议问题