Reputation: 96581
Is it fair to say that Controller defined as part of MainWindow.xib is application's root controller?
Additionally, is true to assume that RootController is always the one responsible for what view is being shown to the user?
Upvotes: 1
Views: 1831
Reputation: 39306
That happens to be the case with some standard IB window based projects but, ultimately you need a window and a view to show something to the user.
Just the view:
Consider this. I created an empty project, add a view (just MyView.xib), add a button to that and this code. No root controllers - just the window and the view.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UINib *nib = [UINib nibWithNibName:@"MyView" bundle:nil];
UIView *myView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
[[self window] addSubview:myView];
[self.window makeKeyAndVisible];
return YES;
}
For a typical window based:
The -info.plist points to MainWindow.xib (Main nib file base name), the File Owner points to the app delegate, the app delegate's viewController points to a UIViewController. Then, typically, window rootviewController is set to the viewController set above.
- (BOOL)application:(UIApplication *)application didFinis hLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
But, if you look at this navigation based app (MasterDetail project), there is no MainWindow.xib.
main.m points to the appDelegate.
the app delegate create the master controller in a navigationController and the navigationController that was create programmatically becomes the rootViewContoller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And finally, in this programmatic example the windows rootViewController is not even set.
The view of the navigation controller is added directly to the window. At the end of the day, the window is just hosting a view. You can set it or the root controller can control it.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// create window since nib is not.
CGRect windowBounds = [[UIScreen mainScreen] applicationFrame];
windowBounds.origin.y = 0.0;
[self setWindow:[[UIWindow alloc] initWithFrame:windowBounds]];
// create the rootViewController
_mainViewController = [[MainViewController alloc] init];
// create the navigationController by init with root view controller
_navigationController = [[UINavigationController alloc] initWithRootViewController:_mainViewController];
// in this case, the navigation controller is the main view in the window
[[self window] addSubview:[_navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Upvotes: 4