iosswiftrootviewcontrollerdismissviewcontroller

Issue on dismissing a viewController to rootViewController


I am trying to dismiss a viewController to rootViewController while signOut. But the problem is that the viewController is not getting dismissed, It still remains in the same page itself. Below I have mentioned the code that I have used.

    let AppDel = UIApplication.shared.delegate as! AppDelegate
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let login = mainStoryboard.instantiateViewController(withIdentifier: "login")
    let nav = UINavigationController(rootViewController: login)
    AppDel.window!.rootViewController = nav
    AppDel.window?.rootViewController?.dismiss(animated: true, completion: nil)
    (AppDel.window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: true)
    login.navigationController?.setNavigationBarHidden(true, animated: false)

Thanks in advance.


Solution

  • Earlier, I have faced the same issue. I was fixed issue by performing all other operation after dismissing controller successfully.

    Please refer below sample code. I am sure it will work for you.

        AppDel.window?.rootViewController?.dismiss(animated: true, completion: {
            (AppDel.window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: true)
            login.navigationController?.setNavigationBarHidden(true, animated: false)    
        })