gpt4 book ai didi

objective-c - rootViewController 需要澄清

转载 作者:行者123 更新时间:2023-12-01 18:32:10 25 4
gpt4 key购买 nike

可以公平地说,作为 MainWindow.xib 一部分定义的 Controller 是应用程序的根 Controller 吗?

此外,假设 RootController 始终是负责向用户显示什么 View 的人是真的吗?

最佳答案

一些标准的基于 IB 窗口的项目恰好就是这种情况,但最终您需要一个窗口和一个 View 来向用户显示某些内容。

只是 View :

考虑一下。我创建了一个空项目,添加了一个 View (只是 MyView.xib),添加了一个按钮和这段代码。没有根 Controller ——只有窗口和 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;
}

对于基于典型窗口:

-info.plist 指向 MainWindow.xib(主 nib 文件基名),File Owner 指向应用委托(delegate),应用委托(delegate)的 viewController 指向 UIViewController。然后,通常将窗口 rootviewController 设置为上面设置的 viewController。
- (BOOL)application:(UIApplication *)application didFinis hLaunchingWithOptions:    (NSDictionary *)launchOptions
{

self.window.rootViewController = self.viewController;

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

main.m 指向 appDelegate。

应用程序委托(delegate)在 navigationController 中创建主 Controller ,并且以编程方式创建的 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。
导航 Controller 的 View 直接添加到窗口中。归根结底,窗口只是托管一个 View 。您可以设置它或根 Controller 可以控制它。
- (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;
}

关于objective-c - rootViewController 需要澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7734838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com