Search code examples
iphoneiosuiviewcontrollerbackwards-compatibilityuiwindow

Backward compatibility of UIWindow rootViewController


When creating a new Xcode 4 view based project, it creates that line on the applicationDelegate didFinishLaunching method :

self.window.rootViewController = self.viewController;

But the attribute rootViewController only appears with iOS 4.0.

What may I write here to be compatible with iOS prior to 4.0?

P.S. : Because of some problems, I won't be able to test the solution, so please it should be ok.


Solution

  • Use introspection to detect if UIWindow class has that property at runtime and if it doesn't, just add view controller's view as subview to the window.

    if ([UIWindow instancesRespondToSelector:@selector(rootViewController)]) {
        self.window.rootViewController = self.viewController;
    } else {
        [self.window addSubview:self.viewController.view];
    }