Quirk of the day - UIWindow rootViewController

Opening up 'old' code is always fun. This quirk is something I came across looking at some code I wrote in 2011. The code is in the App Store and works fine, but when I opened it up in Xcode and compiled it screen rotation was glitchy and I got the warning:

Application windows are expected to have a root view controller at the end of application launch

Fair enough. I certainly thought I had a root view controller by the end application launch. When I checked in application:didFinishLaunchingWithOptions: in the app delegate I could see the view controller being created and it's view being added as a subview of the window's view:

    _rootViewController= [[OSRootViewController alloc] init];
    [self.window addSubview:[_rootViewController view]];

Now "This is how I have always done it" and it seems to have been fine. I checked back in one of the Big Nerd Ranch books which had been my introduction to iOS coding, and they were using the same pattern.

Obviously the world has moved on and something more is needed. If in doubt, read the doco. In the UIWindow documentation I found the rootViewController property. Sure enough, assigning my view controller to this property (rather than adding the view controller's view as a subview) did the trick:

    self.window.rootViewController= _rootViewController;

Mystery solved. Yet another one of those little time consuming things to chase down.

Happy coding.