简体   繁体   English

当导航栏的内置后退按钮被按下时弹出到根视图控制器

[英]Pop to the root view controller when the navigation bar's built in back button is pressed

Whenever the back button is pressed on my top view controller I want to go back to a specific view controller that is not the previous view controller.每当在我的顶视图控制器上按下后退按钮时,我想返回到一个特定的视图控制器,而不是前一个视图控制器。 I was hoping there was a way to override the functionality of the back button in some way.我希望有一种方法可以以某种方式覆盖后退按钮的功能。

EDIT:编辑:

I got this to work:我得到了这个工作:

self.navigationController?.popToRootViewController(animated: true)

The only issue is as it is animating it shows the intermediate view controller as its going to the root.唯一的问题是因为它正在动画它显示中间视图控制器作为它的根。

Reading your edited question, it seems that what you are looking for is to pop back to the root view controller in the navigation stack without the animation revealing any view controllers in between.阅读您编辑的问题,您似乎正在寻找的是弹出回到导航堆栈中的根视图控制器,而动画不会显示其间的任何视图控制器。

You can easily achieve this by removing all those middle view controllers from the stack.您可以通过从堆栈中删除所有这些中间视图控制器来轻松实现这一点。

In the viewDidLoad method of your last shown view controller do:在上次显示的视图控制器的viewDidLoad方法中,请执行以下操作:

func viewDidLoad() {
    super.viewDidLoad()
    if let rootVC = navigationController?.viewControllers.first {
        navigationController?.viewControllers = [rootVC, self]
    }
}

Then, when tapping on the back button, the navigation controller will dismiss the current view controller going back to the root.然后,当点击后退按钮时,导航控制器将关闭当前视图控制器返回到根。

Based on apple developer documentation :基于苹果开发者文档

A navigation controller object manages its child view controllers using an ordered array, known as the navigation stack.导航控制器对象使用称为导航堆栈的有序数组管理其子视图控制器。 The first view controller in the array is the root view controller and represents the bottom of the stack.数组中的第一个视图控制器是根视图控制器,代表堆栈的底部。 The last view controller in the array is the topmost item on the stack, and represents the view controller currently being displayed.数组中的最后一个视图控制器是堆栈中最顶层的项目,代表当前显示的视图控制器。 You add and remove view controllers from the stack using segues or using the methods of this class .您可以使用 segues 或使用此类的方法从堆栈中添加和删除视图控制器

for achieving the pop without animation you should do:为了在没有动画的情况下实现流行,您应该这样做:

  • access viewControllers property which is UINavigationController stack, but you can pop from every where in this array.访问 viewControllers 属性,它是 UINavigationController 堆栈,但您可以从该数组中的每个位置弹出。

    var viewControllers: [UIViewController] The view controllers currently on the navigation stack. var viewControllers: [UIViewController] 当前在导航堆栈上的视图控制器。

example:例子:

yourViewController.navigationController.viewControllers = [specificVC,self]
  • It is better to create an extension and call it wherever you want:最好创建一个扩展并在任何你想要的地方调用它:

Implement:实施:

import UIKit

extension: UIViewController {

    func setNext(to root: UIViewController? = nil) {
        guard let navigation = self.navigationController,
            let first = navigation.viewControllers.first else {return}
        if let root = root {
            navigation.viewControllers = [root,self]
        }else{
            navigation.viewControllers = [first,self]
        }
    }
}

Usage:用法:

self.setNext(to: specificVC)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迅速按下导航栏后退按钮时重新加载视图控制器2 - Reloading view controller when navigation bar back button is pressed in swift 2 弹出到根视图 controller 时导航栏消失 - Dissapearing navigation bar when pop to root view controller 在导航控制器中按下“后退”按钮时,是否可能不取消分配视图控制器? - is it possible to not dealloc a view controller when the back button is pressed in a navigation controller? 当按下导航后退按钮时,将值返回到呈现视图控制器 - Return values to presenting view controller when navigation back button pressed 切换回root View控制器时导航栏出现问题 - Issue with navigation Bar when switching back to root View controller 选项卡栏,当来自另一个选项卡的视图控制器时,弹出到根视图控制器(默认vc) - Tab Bar, pop to root view controller(default vc) when coming from another tab's view controller 按下后退按钮,当我将视图控制器从堆栈中弹出时,将丢失我的自定义UIToolBar - Back button pressed, losing my custom UIToolBar when i pop the view controller off the stack 按下导航控制器后退按钮时如何导航到其他View Controller? - How to navigate to some other View Controller when Navigation Controller back button is pressed? 按下导航控制器的左键按钮时如何停止视图消失 - how to stop view disappearing when navigation controller left bar button pressed 如何在按下按钮时使按钮弹出警报视图并返回主视图控制器? - How Do I Make A Button Get An Alert View To Pop Up When Pressed And Go Back To The Main View Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM