简体   繁体   English

弹出到根导航控制器 - Swift 3

[英]Popping to root navigation controller - Swift 3

I have multiple navigation controllers. 我有多个导航控制器。 They are placed like this hierarchy : 它们像这样的层次结构放置:

(A)SWRevealViewController
       (B)Navigation Controller(sw_front) 
            (C)HomeViewController (root view controller)
                  (D)TabBarController -->
                            (E)Navigation Controller 1--> 
                                 (F)TableController 1 -->
                                 (F)TableController 2.
                            (E)Navigation Controller 2--> 
                                 (F)TableController 1 -->
                                 (F)TableController 2.

I want to pop to the root view controller of the main navigation controllers, ie, I want to go from F to C or B. I have referred to this answer, and also converted it to swift as follows : 我想弹出主导航控制器的根视图控制器,即我想从F转到C或B.我已经提到了这个答案,并将其转换为swift,如下所示:

let appdel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
    let mainwindow : UIWindow = appdel.window!
    let vcObj : HomeViewController = HomeViewController()
    let navObj : UINavigationController = UINavigationController(rootViewController: vcObj)
    mainwindow.rootViewController = navObj

but it just shows a blank black screen. 但它只是显示一个空白的黑屏。 Please can anyone help. 请任何人都可以帮忙。

I am using Xcode 8 and Swift 3. 我正在使用Xcode 8和Swift 3。

Instead of pushing your tabBarVC using sw.pushFrontViewController(VC, animated: true) try using navigationController 's push method, 而不是使用sw.pushFrontViewController(VC, animated: true)推送tabBarVC尝试使用navigationController的推送方法,

self.navigationController?.pushViewController(tabBarVC!, animated: true)

Then from TableController call following to go to HomeViewController , 然后从TableController调用以下转到HomeViewController

func goToHomeNavVC(){
        //parent is TabBar Controller
        self.parent?.navigationController?.popToRootViewController(animated: true)
    }

Here is a Demo 这是一个演示

You can dismiss to a particular view controller by iterating the presentingViewController hierarchy and then calling dismissViewController on the one you want to dismiss to. 您可以通过遍历驳回到特定视图控制器presentingViewController层次结构,然后调用dismissViewController上要取消的一个。

Example: 例:

func dismissToHome() {
    var vc = self.presentingViewController

    while (vc != nil) {
        if (vc!.isKind(of: HomeViewController.self)) {
            break
        }

        vc = vc?.presentingViewController
    }

    vc?.dismiss(animated: true, completion: {
        print("Dismissed to: ", vc!)
    })
}

The above will call dismissViewController on the HomeViewController which will dismiss the view controller that it presented. 上述将调用dismissViewControllerHomeViewController这将驳回,它呈现的视图控制器。 Since that controller is dismissed, all other child controllers are dismissed by their parents. 由于该控制器被解雇,所有其他儿童控制器都被其父母解雇。

I used 我用了

let modal: UIViewController = self.storyboard?.instantiateViewController(withIdentifier: StoryBoardID) as UIViewController!
self.present(modal, animated: true, completion: nil)

to do something like this. 做这样的事情。 StoryBoradID is the identifier you gave the ViewController. StoryBoradID是您为ViewController提供的标识符。 Maybe this helps in your case. 也许这对你的情况有帮助。

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

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