Henry Brown
Henry Brown

Reputation: 2237

How to present a view controller inside a navigation controller SWIFT

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?

Upvotes: 0

Views: 1476

Answers (2)

keithbhunter
keithbhunter

Reputation: 12324

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)

Upvotes: 3

Phillip Mills
Phillip Mills

Reputation: 31016

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

Upvotes: 0

Related Questions