ios 从 Swift 中的导航后退按钮解开 segue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25358482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unwind segue from navigation back button in Swift
提问by Raghavendra
I have a settings screen, in that I have a table cell. By clicking on that I take to another screen where user can choose an option and I want it back in the previous view controller when back button is pressed.
我有一个设置屏幕,因为我有一个表格单元格。通过单击它,我会转到另一个屏幕,用户可以在其中选择一个选项,并且当按下后退按钮时,我希望它回到上一个视图控制器中。
I can put a custom bar button item, but I want to return to the parent view controller using the back button in the navigation bar rather than with a custom button on the view.
我可以放置一个自定义栏按钮项,但我想使用导航栏中的后退按钮而不是视图上的自定义按钮返回到父视图控制器。
I don't seem to be able to override the navigation back button to point it down to my unwind segue action and since the back button doesn't appear on the storyboard, I cant drag the green Exit button to it
我似乎无法覆盖导航后退按钮以将其指向我的展开转场操作,并且由于故事板上没有出现后退按钮,我无法将绿色退出按钮拖到它上面
Is it possible to unwind a push segue with the back button?
是否可以使用后退按钮解除推动转场?
回答by Big Red
Here's my solution, based on Objective-C code from Blankarsch to this StackOverflow question: How to trap the back button event
这是我的解决方案,基于从 Blankarsch 到此 StackOverflow 问题的 Objective-C 代码:How to trap the back button event
Put this code inside the View Controller you want to trap the Back button call from:
将此代码放在要捕获来自以下位置的后退按钮调用的视图控制器中:
override func didMoveToParentViewController(parent: UIViewController?) {
if (!(parent?.isEqual(self.parentViewController) ?? false)) {
println("Back Button Pressed!")
}
}
Inside of the if block, handle whatever you need to pass back. You'll also need to have a reference back to calling view controller as at this point most likely both parent and self.parentViewController are nil, so you can't navigate the View Controller tree.
在 if 块内,处理您需要传回的任何内容。您还需要返回调用视图控制器的引用,因为此时 parent 和 self.parentViewController 很可能都为零,因此您无法导航视图控制器树。
Also, you might be able to get away with simply checking parent for nil as I haven't found a case where pressing the back button didn't result in parent being nil. So something like this is a bit more concise:
此外,您可能可以通过简单地检查父项是否为零而逃脱,因为我还没有发现按下后退按钮不会导致父项为零的情况。所以这样的事情更简洁一点:
override func didMoveToParentViewController(parent: UIViewController?) {
if (parent == nil) {
println("Back Button Pressed!")
}
}
But I can't guarantee that will work every time.
但我不能保证每次都有效。
回答by John R Perry
Do the following in the view controller that hasthe back button
在具有后退按钮的视图控制器中执行以下操作
Swift 3
斯威夫特 3
override func didMove(toParentViewController parent: UIViewController?) {
if !(parent?.isEqual(self.parent) ?? false) {
print("Parent view loaded")
}
super.didMove(toParentViewController: parent)
}
回答by zisoft
I tried the same and it seems that you cannot catch the standard back button's action so the solution will be to use a custom button and bind it to a segue which leads back to the previous page.
我尝试了相同的方法,但似乎您无法捕捉标准后退按钮的操作,因此解决方案是使用自定义按钮并将其绑定到返回上一页的 segue。
回答by joakim
You could use some sort of delegation as you did or use a custom back button and an unwind segue.
您可以像以前一样使用某种委托,或者使用自定义后退按钮和展开转场。
Better even, you could handle passing data between your view controllers using a mediator:
更好的是,您可以使用中介器处理视图控制器之间的数据传递:
http://cocoapatterns.com/passing-data-between-view-controllers/
http://cocoapatterns.com/passing-data-between-view-controllers/