可以说,定义为MainWindow.xib一部分的控制器是应用程序的根控制器吗?
此外,假设RootController总是负责显示给用户的视图,这是真的吗?
发布于 2011-10-12 13:19:44
这恰好是一些基于标准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 (主nib文件基名),文件所有者指向应用委派,应用委派的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;
},最后,在这个编程示例中,甚至没有设置windows 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;
}https://stackoverflow.com/questions/7734838
复制相似问题