rootViewController澄清需要

rootViewController澄清需要

问题描述:

公平地说,定义为MainWindow.xib的一部分的Controller是应用程序的根控制器?rootViewController澄清需要

此外,假设RootController始终是负责向用户显示什么视图的人员,这是否属实?

+1

并不总是如此。我偶尔会将一个rootviewcontroller设置为navigationController。在项目设置中,您可以指定启动时首先加载的Xib。但除此之外,是的,rootviewcontroller可以布局子视图并弹出给其他人,但是当试图决定向用户呈现什么视图时,导航控制器才是真正的优势。 – CodaFi

+0

请以“答复” – JAM

这恰好出现在一些基于标准IB窗口的项目中,但最终需要一个窗口和一个视图来向用户显示某些内容。

就认为:

考虑这一点。我创建了一个空项目,添加一个视图(只是MyView.xib),添加一个按钮到这个和这个代码。没有根控制器 - 只是窗口和视图。

- (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; 
} 

对于基于一个典型的窗口:

的-info.plist点的MainWindow.xib(主要笔尖文件基本名称),文件所有者指向应用程序的委托,应用程序委托的的viewController指向一个UIViewController。然后,通常,将窗口rootviewController设置为上面设置的viewController。

- (BOOL)application:(UIApplication *)application didFinis hLaunchingWithOptions: (NSDictionary *)launchOptions 
{ 

    self.window.rootViewController = self.viewController; 

但是,如果你看这个导航基于应用程序(MasterDetail项目),没有MainWindow.xib文件。

main.m指向appDelegate。

应用程序的委托创建一个navigationController和navigationController主控制器,这是编程创建成为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; 
} 

最后,在此方案示例中的窗口RootViewController的甚至没有设置。
导航控制器的视图直接添加到窗口。在一天结束时,窗户只是托管一个视图。您可以设置它或者根控制器可以控制它。

- (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; 
}