MoeinDeveloper
MoeinDeveloper

Reputation: 261

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??

Upvotes: 0

Views: 141

Answers (2)

Alirza Eram
Alirza Eram

Reputation: 178

you may try this:

let sb = UIStoryboard(name: "1", bundle: nil)
let vc = sb.instantiateViewController(identifier: "ID") as! LoginVC
    
if let nav = navigationController {
    nav.pushViewController(vc, animated: true)
}else {
    self.present(vc, animated: true, completion: nil)
}

Upvotes: 0

Yury Imashev
Yury Imashev

Reputation: 2128

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
}

Upvotes: 1

Related Questions