ppp
ppp

Reputation: 771

self.navigationController is always nil

UI flow:

AppDelegate

-->

LoginViewController (not in the storyboard)

-->

navigation controller (in the storyboard)

-->

PFQueryTableViewController (in the storyboard) named "OrdersVC"

This is the navigation controller with OrdersVC:

enter image description here

This is my AppDelegate:

var window: UIWindow?

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

    // initial VC
    let VC = LoginViewController()
    
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.rootViewController = VC
    window!.makeKeyAndVisible()
    
    return true
}

The above works fine. Then, from LoginViewController, I am then trying to display my storyboard's initial VC which is a navigation controller hosting a PFQueryTableViewController. Note that LoginViewController is not in the storyboard.

let destVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("OrdersVC") as! UITableViewController

// will throw "unexpectedly found nil"     
let navController = UINavigationController(rootViewController: destVC)
navController.pushViewController(destVC, animated: true)

// will throw "unexpectedly found nil"
self.presentViewController(navController, animated: true, completion: nil)

Problem is that in my PFQueryTableViewController's viewDidLoad and viewDidAppear the following statement is always nil:

// navitaionController is nil
self.navigationController!.navigationBar.translucent = false

So how can I correctly instantiate PFQueryTableViewController inside its navigation controller?

Upvotes: 3

Views: 7424

Answers (1)

Mundi
Mundi

Reputation: 80271

You are instantiating the OrdersVC instead of instantiating the navigation controller into which it is embedded and which is the "initial" view controller of your storyboard. Use instantiateInitialViewController instead of using the identifier.

let nav = storyboard.instantiateInitialViewController()
self.window!.rootViewController = nav

The reason for the confusion is that you are "unlinking" the initial view controller from the storyboard with your login controller. You have to add the initial view controller back to the main window.

Upvotes: 5

Related Questions