rootViewController 需要澄清

发布于 2024-12-09 08:58:39 字数 107 浏览 0 评论 0原文

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

此外,是否真的假设 RootController 始终负责向用户显示哪个视图?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-12-16 08:58:40

某些基于标准 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文