iosswiftuinavigationcontrolleruistoryboard

Push a specific viewController


I have 2 storyboards and in storyboard 1 I have a login page, in the storyboard 2, I have my main page! So the problem is in my main page, i have button for logging out, and when the user press the button I want to go back to my login page, I have already tried some methods that doesn't work for me! I tried:

self.dismiss(animated: true, completion: nil)

And:

for controller in self.navigationController!.viewControllers as Array { if controller.isKind(of: ViewController) { self.navigationController!.popToViewController(controller, animated: true) break } }

So, any ways to get back to my login page??


Solution

  • Since your code doesn't work, I suppose that your login view controller is not in navigation stack. Here's what you can try.

    let storyboard = UIStoryboard(name: "Login", bundle: nil)
    
    if let loginViewController = storyboard.instantiateInitialViewController() {
        (UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = loginViewController
    }