3

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?

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
ppp
  • 713
  • 1
  • 8
  • 23
  • 1
    Can you give a clearer explanation of the view controller hierarchy involved here? Ultimately, `self.navigationController` is `nil` "if the view controller is not embedded inside a navigation controller". Perhaps you are embedding a different view controller than you think you are? – Aaron Brager Jan 25 '16 at 22:20
  • tried to add more clarity, see revised Q please. is it possible that I get nil because I am trying to instantiate a navigation controller that's set to be the initial view controller of the storyboard? – ppp Jan 25 '16 at 23:13

1 Answers1

5

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.

Mundi
  • 79,884
  • 17
  • 117
  • 140