0

I am trying to present a UIViewController from the Storyboard (no UINavigationController here)

When I start my app, I print out:

print("self.view.window.rootViewController : \(UIApplication.sharedApplication().windows.first?.rootViewController)")
print("self : \(self)")

Output:

self.view.window.rootViewController : Optional(<NewProject.MainViewController: 0x7f95d0d8bcc0>)
self : <NewProject.MainViewController: 0x7f95d0d8bcc0>

My MainViewController is indeed the rootViewController. Good!

Then I want to present another UIViewController and I do:

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("testViewController") as! TestViewController

presentViewController(vc, animated: true, completion: nil)

And I get an error:

Warning: Attempt to present <NewProject.testViewController: 0x7f95d0d92a20> 
on <NewProject.MainViewController: 0x7f95d0d8bcc0> whose view 
is not in the window hierarchy!

What's wrong? I'm trying to present a view controller from the root view controller.

Nico
  • 6,269
  • 9
  • 45
  • 85
  • 1
    is the window that MainViewController is in the key window? – Will M. Feb 10 '16 at 21:27
  • http://stackoverflow.com/a/12320222/4475605 – Adrian Feb 10 '16 at 21:28
  • @WillM. No, it's set to false. I guess that's the problem? – Nico Feb 10 '16 at 21:32
  • @AdrianB, thanks for the link, indeed I'm calling it from `viewDidLoad`. The problem is that I don't want to call it in `viewDidAppear` as I don't want to show this viewController if some conditions are gathered. – Nico Feb 10 '16 at 21:33
  • @Nico for what reason can you not put it in viewDidAppear? – Will M. Feb 10 '16 at 21:34
  • Because on my first view controller, I'm checking whether or not the user is already logged in. If he is, I don't want to show the login view. I guess it's not the right way to do it. – Nico Feb 10 '16 at 21:37
  • I'll probably do the checking in the AppDelegate and choose the rootViewController from there. – Nico Feb 10 '16 at 21:40

0 Answers0