xcode 如何强制后退按钮转到第一个视图控制器 (swift)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29036138/
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
How to Force Back Button to Go to First View Controller (swift)
提问by Artis Universe
I have storyboard of several view controllers embedded in Navigation Controller. Due to navigation logic in later views of storyboard, the back button (in the left upper corner, in navigation bar) does not go back to the first view. I am wondering where and how to change this behaviour of Back button of second view only. Appreciate any ideas, examples.
我在导航控制器中嵌入了几个视图控制器的故事板。由于故事板后面视图中的导航逻辑,后退按钮(位于导航栏中的左上角)不会返回到第一个视图。我想知道在何处以及如何仅更改第二个视图的后退按钮的这种行为。欣赏任何想法,例子。
回答by MarkHim
Of course you could implement a custom back button. But there is also a nice way to keep using the default button.
当然,您可以实现自定义后退按钮。但是还有一个很好的方法可以继续使用默认按钮。
Simply check if the current viewController is still in the navigation stack in viewWillDisappear
before you call super.viewWillDisappear()
. If it is not, the back button has been pressed. Then you can do the popToRootViewControllerAnimated
.
在viewWillDisappear
调用之前,只需检查当前 viewController 是否仍在导航堆栈中super.viewWillDisappear()
。如果不是,则后退按钮已被按下。然后你可以做popToRootViewControllerAnimated
.
override func viewWillDisappear(animated: Bool) {
if (navigationController?.viewControllers)!.contains(self) {
// back button was pressed
self.navigationController?.popToRootViewControllerAnimated(animated)
}
super.viewWillDisappear(animated)
}
回答by Mister Orko
The custom back button appears to be the best solution. The code inside your action method would look like the following in swift:
自定义后退按钮似乎是最好的解决方案。您的操作方法中的代码在 swift 中如下所示:
self.navigationController?.popToRootViewControllerAnimated(true)
//Just change the true to false if you don't want it animated.
//如果您不想动画,只需将 true 更改为 false。
I hope this helps (if you had not already found the answer). Cheers!
我希望这会有所帮助(如果您还没有找到答案)。干杯!
回答by Chanchal Raj
If you meant to say: Prevent Back Button Navigating to Previous Controller and move to first view controller: You could do this by creating a custom back button - drag a button in storyboard to the top left of the navigation bar, and wire it up to your view controller. In your custom back button's selector write:
如果您的意思是:防止后退按钮导航到上一个控制器并移动到第一个视图控制器:您可以通过创建自定义后退按钮来实现 - 将故事板中的按钮拖到导航栏的左上角,并将其连接到你的视图控制器。在自定义后退按钮的选择器中写入:
[self.navigationController popToRootViewControllerAnimated:YES]
回答by ryancrunchi
Assuming you have a view controllers stack like : VC1 > VC2 > VC3, and you want to back to VC1 when tapping back on VC3, then you could set this code in VC2:
假设您有一个视图控制器堆栈,如:VC1 > VC2 > VC3,并且您想在点击 VC3 时返回到 VC1,那么您可以在VC2 中设置此代码:
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:(UIBarButtonItemStylePlain) target:self action:@selector(backToVC1)]];
Then, always in VC2 :
然后,始终在 VC2 中:
- (void)backToVC1
{
[self.navigationController popToRootViewControllerAnimated:YES];
}