简体   繁体   English

弹出到根视图控制器

[英]popping to root view controller

I have an alert whose OK button should pop to the root view controller. 我有一个警报,其“确定”按钮应弹出到根视图控制器。

Here's the structure of what I'm working with: 这是我正在使用的结构:

[tab view controller] -> [navigation controller] -> [view controller] -> [view controller] -> [navigation controller] -> [view controller] -> [alert] [选项卡视图控制器]-> [导航控制器]-> [视图控制器] -> [视图控制器] -> [导航控制器]-> [视图控制器]-> [警告]

I would like the OK button on the alert take me to the view controller I've bolded above. 我希望警报上的“确定”按钮将我带到我上面已加粗的视图控制器。 When I do the code below, I go back to the view controller I've italicized above, which isn't quite what I want. 当我执行下面的代码时,我回到上面斜体的视图控制器,这并不是我想要的。 Any help would be much appreciated! 任何帮助将非常感激! Thanks! 谢谢!

alert.addAction(UIAlertAction(title: OK, style: UIAlertActionStyle.default, handler: { action in

    DispatchQueue.main.async(execute: {
        _ = self.dismiss(animated: true, completion: nil)

    })
}))

I have also tried using the special method for popping to the root view controller, but this has not worked, sadly. 我还尝试了使用特殊方法弹出到根视图控制器,但是遗憾的是,这没有用。

The UINavigationViewController class has a method func popToRootViewController(animated: Bool) -> [UIViewController]? UINavigationViewController类具有方法func popToRootViewController(animated: Bool) -> [UIViewController]? ( Documentation ) 文件

You can just call this method on the fist navigation view controller. 您可以只在第一个导航视图控制器上调用此方法。 (Note: Therefore you need to have a reference to this navigation view controller or delegates to call this method) (注意:因此,您需要引用此导航视图控制器或委托以调用此方法)

If you use a Storyboard you can use a segue to unwind to the correct view controller. 如果使用情节提要,则可以使用segue展开到正确的视图控制器。 Therefore see this post . 因此请参阅这篇文章

You need to navigate the particular index of navigate stack. 您需要浏览导航堆栈的特定索引。 Eg:- First get the count of view controllers present in navigation stack. 例如:-首先获取导航堆栈中存在的视图控制器的数量。 Pop to particular index. 弹出特定索引。 In you case may be its index is 0: 在您的情况下,它的index可能是0:

let arr_controller:[UIViewController] = (self.navigationController?.viewControllers)!
_ = self.navigationController?.popToViewController(arr_controller[0], animated: true)
let tabVc = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController
tabVc?.selectedViewController?.navigationController?.popToRootViewController(animated: true)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM