如在正式文件"Pushing and popping stack items"
func popToRootViewControllerAnimated(_ animated: Bool) -> [UIViewController]?
彈出堆棧除了根視圖 控制器上的所有視圖控制器和更新顯示說明。
你可以用它簡單地用:
@IBAction func backToRootButton(sender: UIButton) {
navigationController?.popToRootViewControllerAnimated(true)
}
此圖片可以說明,當你需要去RootViewController的:
有了這個代碼下面你可以定製你的navigationController 後退按鈕:
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
self.navigationItem.leftBarButtonItem = newBackButton;
}
func back(sender: UIBarButtonItem) {
// Perform your custom actions
// ...
// Go back to the previous ViewController
//self.navigationController?.popViewControllerAnimated(true)
// ..or go back to the rootViewController
self.navigationController?.popToRootViewControllerAnimated(true)
}
你決定是否需要簡單地彈出到先前的viewController或直接去根,取決於你的堆棧有多深,或者你在這一刻是誰topViewController ..
你可以檢查「unwind segue」,我想這正是你要找的 –