Ashot Khachatryan
Ashot Khachatryan

Reputation: 326

Push view controller not working in Swift

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let playGameViewController = (storyboard.instantiateViewController(withIdentifier: "PlayGameViewController") as! PlayGameViewController)
        self.navigationController?.pushViewController(playGameViewController, animated: true)

push not worked but I tried present working, I want navigate with push.

Upvotes: 0

Views: 3511

Answers (1)

Frankenstein
Frankenstein

Reputation: 16361

Try running the below code to know where you have gone wrong.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let playGameViewController = storyboard.instantiateViewController(withIdentifier: "PlayGameViewController") as? PlayGameViewController else {
    print("This means you haven't set your view controller identifier properly.")
    return
}
guard let navigationController = navigationController else {
    print("This means you current view controller doesn't have a navigation controller")
    return
}
navigationController.pushViewController(playGameViewController, animated: true)

Try using breakpoints to figure out if any variable is nil. In your case, there is more probability for having your navigationController being nil.

Upvotes: 4

Related Questions