Ugo Ugo - 1 year ago 110
Swift Question

NavigationController nil with Material

I have this in appDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

window = UIWindow(frame: UIScreen.mainScreen().bounds)
let nav = NavigationViewController(rootViewController: MenuViewController(rootViewController: FeedVC()))
window?.rootViewController = SideNavigationController(rootViewController: nav, leftViewController: LeftVC(), rightViewController: nil)

This works fine. My FeedVC is displayed and I can access the LeftVC with SideNavigationController. (Btw, is it a good way of doing it ?)

Then in my FeedVC class, I want to add titleLabel (navigationItem) to the navigationBar but navigationController is nil !

So I think my FeedVC is not embed in NavigationController. But I'm not using storyboard for this. How can I do this programmatically and take in count that there is also a SideNavigationController at the very top...

Thanks for helping!

Answer Source

Yes, all looks good, except, you do not need to set the rightViewController to nil. You can leave it out completely.

The issue with the titleLabel is that your FeedVC is a child of the MenuViewController. The MenuViewController is a child of the Nav, so the navigationItem should be set in the MenuViewController for this reason. You could flip the MenuViewController and NavigationController relationship, so that the Nav is a child of the Menu. Then you would be able to set the FeedVC navigationItem accordingly.

Hope that helps :)

Also, the framework is Material, not Material Kit. Might be better for search to correct that :)