Application Windows Are Expected To Have A Root View Controller At The End Of Application Launch

Chances are you’ve seen the message shown in the title of this post when viewing the debug console in Xcode: “Application windows are expected to have a root view controller at the end of application launch”

I’ve not experienced a direct issue with an application not working as expected when this warning is present. However, warnings are there for a reason, so in the interest of keeping everything clean at compile time, I’ll show you how to resolve this issue.

Typical startup code in the app delegate follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Create and initialize the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
// Create a view controller
vc = [[SomeViewController alloc] init];
 
// Add subview of view controller to window
[window addSubview:vc.view];
 
// Make key window visible
[window makeKeyAndVisible];
 
// End delegate code...

Here is a quick way to work around the message about no root view controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Create and initialize the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
// Create a view controller
vc = [[SomeViewController alloc] init];
 
// Set the root view controller in the window
[window setRootViewController:vc];
 
// Make key window visible
[window makeKeyAndVisible];
 
// End delegate code...

The change required is to replace line 8 in the original example to reference to the root view controller in the window, which will also provide the content view for the window. By assigning the root view controller in this manner the view controllers view is set as the content view of the window (just as the addSubview call did in the first code example).

  1. It’s worth noting that setRootViewController: not exists in iOS pre 4.0…

  2. The comment is not harmless. I had a universal app without root view controller work fine in iOS 5 and previous. When running the same binary on iOS 6, which introduced rotation changes, some of the rotation stopped working properly for nested hierarchies of views and places were… ugh… maybe I wasn’t doing stuff “the Apple way”.

  3. Yes good point! Actually in my experimenting with this error, if you have a tabbar controller in your application and running on iPhone 5, the tabbar will not respond to touch events unless to implement the above change (I dont use interface builder at all so that might be a factor in this behaviour)

  4. You can also do it in IB, select the Window and drag from the inspector to your TabBarController

Comments are closed.