Mike Jonas
Mike Jonas

Reputation: 333

Embed view controller in UINavigationController

Initially i just presented the view like this and it worked fine.

let editSpotVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("EditSpotViewController") as! EditSpotViewController

self.presentViewController(editSpotVc, animated: false) { () -> Void in
    editSpotVc.addImage(ImageTransformationUtil.scaleImageTo(newWidth: 1080, image: image))
    editSpotVc.updateMapAndReverseGeocode(photoCoordiantes)
}

I need to have the view controller embedded in a navigation controller now. My solution (below) doesn't work. The program crashes because it’s finding nil when it runs the editSpotVC methods.

let editSpotVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("EditSpotViewController") as! EditSpotViewController
let editSpotNavVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("EditSpotNavController")

self.presentViewController(editSpotNavVc, animated: false) { () -> Void in
    editSpotVc.addImage(ImageTransformationUtil.scaleImageTo(newWidth: 1080, image: image))
    editSpotVc.updateMapAndReverseGeocode(photoCoordiantes)
}

How can I make this work smoothly?

Thanks!

Upvotes: 0

Views: 131

Answers (1)

Zigii Wong
Zigii Wong

Reputation: 7826

1. Go to Storyboard -> Select the viewController you want to embed -> Editor -> Embed in -> Navigation Controller

2.

let nav = UINavigationController(rootViewController: yourViewController)

In this case,

let editSpotVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("EditSpotViewController") as! EditSpotViewController
let navigationController = UINavigationController(rootViewController: editSpotVc)

Upvotes: 1

Related Questions