How to present a view controller inside a navigati

2019-08-29 19:07发布

问题:

I have VC1 which is a basic view controller, I then want to present VC2 which is inside a navigation controller. Whenever I present it, it doesn't display the navigation controller. I want this all done pragmatically. The code I have been using to present VC2 is:

func matchesPressed(sender: UIButton!) {
    let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches
    self.presentViewController(matchesTVC, animated: true, completion: nil)

}

How do I present the navigation controller it is inside as well?

回答1:

Is the navigation controller in the storyboard? If so, give it a storyboard ID and replace your matchesTVC stuff with the navigation controller.

If matches is a standalone view controller in the storyboard, you can do it in code like this:

let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches 
let navContr = UINavigationController(rootViewController:matchesTVC)
self.presentViewController(navContr, animated: true, completion: nil)


回答2:

Instantiate the navigation controller that contains your view controller and present the navigation controller.