在导航控制器中覆盖后退按钮(Override Back Button In Navigation Controller)

编程入门 行业动态 更新时间:2024-10-24 18:27:08
在导航控制器中覆盖后退按钮(Override Back Button In Navigation Controller)

我有这些流程的故事板。 我正在使用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

从每个流的View Controller Target中 ,我想覆盖它的后退按钮,以便它可以返回到View Controller Initial(Home)并将其对象释放到内存中。

有没有可能的方法来做到这一点? 任何帮助,将不胜感激。 谢谢。

I have storyboard with these flow. I am using SWRevealViewController to navigate to each navigation controller.

- 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

From View Controller Target on each flow, I want to override its back button so it could back to View Controller Initial (Home) and release its object on memory.

Is there any possible way to do this? Any help would be appreciated. Thank you.

最满意答案

首先你需要用自定义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)

First you need to replace your back button with custom Back BarButton with it selector.

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .Done, target: self, action: #selector(self.backToInitial(_:))) func backToInitial(sender: AnyObject) { self.navigationController?.popToRootViewControllerAnimated(true) }

If you are running swift 3.0 then selector syntax is like this.

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

Edit: For SWRevealViewController try like this.

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

更多推荐

本文发布于:2023-07-24 20:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250533.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   器中   Override   Controller   Navigation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!