Ket123
Ket123

Reputation: 21

Swift - 'Pushing a navigation controller is not supported'

I'm trying to push a viewcontroller from a tableviewcell using this code:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "UserPostedViewController") as! UserPostedViewController
    self.navigationController?.pushViewController(vc, animated: true)
}

It's then giving me this error message: 'Pushing a navigation controller is not supported'

I'm also using a XIB file to present the cell. Can anyone help me? Thanks.

Upvotes: 0

Views: 6401

Answers (4)

Abhishek Biswas
Abhishek Biswas

Reputation: 31

use this way

let vc = self.UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "viewController.storyboard.id") as! viewControllerFileName 

self.navigationController?.pushViewController(vc, animated: true)

Upvotes: 0

Arnab
Arnab

Reputation: 4356

If you already have a navigation controller, then why are you trying to push to another navigation controller? Just remove the navigation controller and push to it's root view controller should work. Else if you want you can present the navigation controller from current VC.

You are trying to achieve this:

NavigationController -> SomeVC -> AnotherVC -> NavigationController -> NewVC 

Try to do it like this:

NavigationController -> SomeVC -> AnotherVC -> NewVC 

OR

NavigationController -> SomeVC -> AnotherVC 
                                    | 
                                    | Present
                                    V
                                 NavigationController -> NewVC 

Upvotes: 8

Fahad Malik
Fahad Malik

Reputation: 53

Please check if you are trying to push a navigation controller. if so, 1- Change reference to root of navigation controller 2- Present navigation controller instead

Upvotes: 0

ahbou
ahbou

Reputation: 4918

UINavigationController can't be pushed, they are presented with presentViewController:animated

If you wish to retain the push animation remove the navigation controller from your UserPostedViewController.

Upvotes: 0

Related Questions