在导航控制器中覆盖后退按钮

时间:2016-08-30 09:00:10

标签: ios swift uinavigationcontroller swrevealviewcontroller

我有这些流程的故事板。我正在使用SWRevealViewController导航到每个导航控制器。

- Navigation Controller 1 --> View Controller Initial (Home) --> View Controller Target
- Navigation Controller 2 --> View Controller X --> View Controller Y --> View Controller Target
- Navigation Controller 3 --> View Controller M --> View Controller Target

从每个流上的查看控制器目标,我想覆盖其后退按钮,以便它可以返回查看控制器初始(主页)并在内存中释放其对象。

有没有办法做到这一点?任何帮助,将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:9)

首先,您需要使用自定义Back BarButton替换后退按钮。

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .Done, target: self, action: #selector(self.backToInitial(_:)))

func backToInitial(sender: AnyObject) {
     self.navigationController?.popToRootViewControllerAnimated(true)
}

如果您正在运行swift 3.0,则selector语法就是这样。

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .Done, target: self, action: #selector(self.backToInitial(sender:)))

修改SWRevealViewController尝试这样做。

let revealController = self.revealViewController;
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("InitialViewController") as! InitialViewController
let navigationController = UINavigationController(rootViewController: vc)
revealController.pushFrontViewController(navigationController, animated:true)

答案 1 :(得分:0)

对于View Controller Target中的backButton操作,请使用:

[self.navigationController popToRootViewControllerAnimated:YES];