I\'ve noticed a lot of examples for iPhone apps in the Application Delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
h
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];
}
The UIWindow
rootViewController
property is new with iOS4.
The older technique was to use addSubview
.
The new, recommended technique is to set rootViewController
.
The crash is because you're calling a method that doesn't exist, not because your variables are not initialized.
-setRootViewController
doesn't exist prior to iOS 4.0. Use
[self.window addSubview:self.tabBarController.view];
instead.
Or, update your target platfor to 4.0.2 or later. It's probably less than 5% of users that aren't using iOS 4 at this point.
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.
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