0

in the viewDidLoad method i have this code. When the application is running its not go to the another viewController, its gives me an error:

Warning: Attempt to present <CompleteCountryViewController: 0x7fb971779be0> on <ViewController: 0x7fb97176f3e0> whose view is not in the window hierarchy!

What can i do, that when the application running its will go to another viewController?

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
Nir Gofman
  • 45
  • 1
  • 7

3 Answers3

3

You should not present a view controller in the viewDidLoad method of another controller because you cannot show a view controller (present modally or push) when a transition is already occurring (push, pop, present, dismiss).

My suggestion is that you move the code in your code sample to the viewDidAppear: method. At this point, you know for sure that the transition has completed.

Roberto
  • 3,487
  • 1
  • 22
  • 24
  • You can give me example? – Nir Gofman Oct 10 '14 at 08:01
  • i add this code and it does'nt do anything: [the code](http://www.codeshare.io/nH71W) – Nir Gofman Oct 10 '14 at 08:05
  • The code you shared includes a method called `viewDidAppear`. The actual method you need to overwrite is `viewDidAppear:` (see semicolon ":"), which has a parameter called `animated`. Please make sure you are overwriting the correct view lifecycle method. – Roberto Oct 10 '14 at 08:33
2

You seem to have a slight misunderstanding of the lifecycle of UIViewController if you want to modally present a view controller inside the viewDidLoad of another one.

viewDidLoad gets called in one view controller after it has been instantiated and its view components have been loaded (thus the name). The view of that view controller is about to be displayed, so it doesn't make much sense to instantiate another view controller at this point and present it on the first one.

Let me give you an example with two view controller A and B.

You instantiate A and its viewDidLoad gets called. So, A is about to be displayed! What you are doing in your code now is to instantiate B at this very point and show it on A. iOS doesn't like that and will give you your error.

nburk
  • 22,409
  • 18
  • 87
  • 132
0

I had an issue where I was attempting to present a modal view controller within the viewDidLoad method. The solution for me was to move this call to the viewDidAppear: method.

View controller's view is not in the window's view hierarchy at the point that it has been loaded (when the viewDidLoad message is sent), but it is in the window hierarchy after it has been presented (when the viewDidAppear: message is sent).