51

I've noticed a lot of examples for iPhone apps in the Application Delegate

- (void)applicationDidFinishLaunching:(UIApplication *)application

have

[window addSubview: someController.view]; (1)

as opposed to

self.window.rootViewController = self.someController; (2)

Is there any practical reason to use one over the other? Is one technically correct? Do controller's have an equivalent command to number (2) like

self.someController.rootController = self.someOtherController; // pseudocode

Gazzer
  • 4,524
  • 10
  • 39
  • 49

4 Answers4

51

The UIWindow rootViewController property is new with iOS4.

The older technique was to use addSubview.

The new, recommended technique is to set rootViewController.

TomSwift
  • 39,369
  • 12
  • 121
  • 149
  • 7
    What are some of the reasons why setting rootViewController is recommended over addSubview? – pepsi Jun 30 '11 at 14:47
  • 1
    @pepsi I can tell you that it makes a lot more sense on iOS 5.0, its under NDA so we cant discuss it here, ask on the dev forums and you will get it, it has to do with view controller containment. – Zebs Jul 08 '11 at 05:18
  • 1
    one arguable benefit is that as a property, you can set it in IB rather than writing code to perform the association. – TomSwift Jul 08 '11 at 18:32
  • 2
    Dont merge both techniques! it will cause weird behaviors and sometimes will cause autorotate to stop working. – Paul N May 25 '12 at 00:44
  • `rootViewController` do not work well when creating a new game center account in your app on iPad - the underlying view disappears. The same thing works well when the main view was added with `addSubview`. – rasmus Jul 11 '12 at 20:24
10

Just an update on this with the release of ios 6.

If still using the -[UIWindow addsubview:] boilerplate, you will probably get the message "Application windows are expected to have a root view controller at the end of application launch" in your console as well now. Along with potential rotation issues and layout issues in your app.

Setting the window's rootViewController as above will fix this too.

jaseelder
  • 3,403
  • 3
  • 25
  • 26
  • Yep, my view was showing up in the wrong orientation even when the ViewController used the new iOS6 rotation apis...until I fixed this! – mattorb Oct 26 '12 at 16:51
  • for God's sake! I am having this message for weeks and could not find a proper solution until now that I was not searching for that! T H A N K S – Duck Nov 05 '12 at 18:48
5

I use this code:

    rootViewController_ = [[RootViewController alloc] initWithFrame:[UIScreen mainScreen].bounds];
    window_ = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    if ([window_ respondsToSelector:@selector(setRootViewController:)]) { // >= ios4.0
        [window_ setRootViewController:rootViewController_];
    } else { // < ios4.0
        [window_ addSubview:rootViewController_.view];
    }
Ron
  • 3,055
  • 1
  • 20
  • 21
1

My Opinion:

self.window.rootViewController will resize the rootViewController.view according to status bar height

But if you use addSubview it won't

For example, if you setRootViewController to a NavigationController, the navigationController would be (0,0,320,480);

but if you setRootViewController to a common UIViewController, the navigationController would be (0,0,320,460);

if you use addSubview: the two viewcontrollers would be (0,0,320,480)

And if there is an In-call-StatusBar. it also change for you when you use setRoot... if you use addSubview, the subview size wouldn't change

do some test with different view border color

aelam
  • 2,796
  • 2
  • 27
  • 32