3
votes

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, i.e, I want to go from F to C or B. I have referred to this answer, and also converted it to swift as follows :

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.

3
let drawerViewController = viewControllerFrom("Main", vcid: "SliderMenuViewController") let dashboardVC = viewControllerFrom("Main", vcid: "UserDashboardViewController") as! UserDashboardViewController let navigationControl = UINavigationController(rootViewController: dashboardVC ) window?.rootViewController = navigationControl t - Himanshu Moradiya
How do you add TabBarController to HomeViewController? - Mohammad Zaid Pathan
@HimanshuMoradiya this doesn't work. There is nothing such as viewControllerFrom() - Dia
@ZaidPathan I added it on the storyboard but accessed it through storyboard ID in code - Dia
@user7205816 But how did you add it? I mean present/push/addChild view? - Mohammad Zaid Pathan

3 Answers

5
votes

Instead of pushing your tabBarVC using sw.pushFrontViewController(VC, animated: true) try using navigationController's push method,

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

Then from TableController call following to go to HomeViewController,

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

Here is a Demo

0
votes

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.

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. Since that controller is dismissed, all other child controllers are dismissed by their parents.

-2
votes

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. Maybe this helps in your case.