Self.window.rootViewController vs window addSubview

I noticed a lot of examples for iPhone apps in app deletion

- (void)applicationDidFinishLaunching:(UIApplication *)application

there is

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

Unlike

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

Is there a practical reason to use one over the other? Is it correct? The controller has an equivalent command for number (2), for example

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

+49
objective-c iphone
Mar 09 '11 at 16:00
source share
5 answers

The UIWindow rootViewController property is new with iOS4.

The old technique was to use addSubview .

The new recommended method is to install rootViewController .

+48
Mar 09 2018-11-11T00:
source share

Just update this with ios 6 release.

If you are still using the template template - [UIWindow addedubview:], you will most likely receive the message "Application windows are now expected to have a root view controller at the end of the application launch" on your console. Along with potential rotation issues and placement issues in your application.

Setting the rootViewController window as described above will also fix this.

+10
Oct 22
source share

I am using 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]; } 
+5
Nov 03 2018-11-11T00:
source share

My opinion:

self.window.rootViewController changes the size of rootViewController.view depending on the height of the status bar

But if you use addSubview, it will not

For example, if you set the RootViewController to a NavigationController, the navigation controller will be (0,0,320,480);

but if you set the RootViewController to a common UIViewController, the navigation controller will be (0,0,320,460);

if you use addSubview: two view managers will be (0,0,320,480)

And if there is an In-call-StatusBar. this will also change for you when you use setRoot ... if you use addSubview, the size of the subview will not change

perform some tests with a different presentation frame color

+1
Sep 10
source share

Failure is because you are calling a method that does not exist, and not because your variables are not initialized.

-setRootViewController does not exist until iOS 4.0. Use

[self.window addSubview:self.tabBarController.view]; instead of this.

Or upgrade your target planet to version 4.0.2 or later. This is probably less than 5% of users who are not using iOS 4 at the moment.

-one
Aug 6 2018-11-12T00:
source share



All Articles