iOS 编程:关于根视图控制器的说明 [英] Programming iOS: clarifications about Root View Controller

查看:12
本文介绍了iOS 编程:关于根视图控制器的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过这个问题,我想知道我是否理解根视图控制器的概念.

Through this question I would like to know if I understand well the notion of Root View Controller.

在 iOS 应用程序中,根视图控制器 (RVC) 是其视图在启动时添加到 UIWindow 应用程序的控制器,不是吗?

In iOS application, the Root View Controller (RVC) is the controller whose view gets added to the UIWindow application at startup, isn't true?

[window addSubview:rvcController.View];
[window makeKeyAndVisible];

现在,一个 UIWindow 也有一个 rootViewController 属性.运行之前的代码片段时,该属性是填充了 rvcController 还是我必须显式设置它?

Now, an UIWindow has also a rootViewController property. When running the previous snippet of code, does that property gets populated with the rvcController or do I have to set it explicitly?

然后,在 UINavigationController 中,可以设置与之前为入口点设置的 RVC 不同的 RVC.

Then, in a UINavigationController it is possible to set a RVC that is different from the previous RVC set for the entry point.

在这种情况下,我第一次将控制器添加到 navigationController 堆栈(在其上推送新控制器)时,框架是否将该控制器设置为 navigationController 的 RVC,或者我必须通过 initWithRootViewController 方法?

In this case, the first time I add a controller to the navigationController stack (pushing a new controller on it), does the framework set that controller as the RVC for the navigationController or do I have to set it explicitly through initWithRootViewController method?

推荐答案

是的..当我开始 iPhone 开发.. rootViewController 的事情也让我陷入了循环.但这真的很简单.

Ya.. when I began iPhone dev.. the rootViewController thing threw me for a loop too. But it’s really straight forward.

当应用程序启动时,我在我的应用程序委托类中创建了一个 UIWindow 对象.另外,在那个类中,我有一个 UIWindow 类型的属性,称为 window;

when the app starts, I create a UIWindow object in my app delegate class. Also, in that class, I have a property of type UIWindow called window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    UIWindow *w = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window=w;
    [w release];
    // other code here...
}

然后我创建一个 UIViewController,它的 view 将是窗口层次结构中的第一个视图,这可以称为根视图控制器".

I then create a UIViewController whose view will be the first view in the window hierarchy, this could be called the "root view controller".

令人困惑的部分是......我们经常创建一个 UINavigationController 作为根视图控制器",并且该导航控制器有一个 init 方法请求RootViewController",这是第一个viewcontroller 它将放置在其堆栈中.

The confusing part is...that often we create a UINavigationController as the "root view controller" and that navigation controller has an init method that asks for a "RootViewController", which is the first viewcontroller it will place on its stack.

因此,窗口获得了一个根视图控制器",即 UINavigationController,它还有一个 RootViewController,这是您要显示的第一个视图控制器.

So, the window gets a "root view controller", which is the UINavigationController, which also has a RootViewController, which is the first view controller you want to show.

一旦你把它弄明白了,一切都说得通了.我认为:-)

once you sort that out, its all makes sense.. I think :-)

这里有一些代码可以完成所有工作..(取自我面前打开的一个项目)

here is some code that does it all.. (taken from a project I have open in front of me)

//called with the app first loads and runs.. does not fire on restarts while that app was in memory
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


    //create the base window.. an ios thing
    UIWindow *w = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window=w;
    [w release];

    // this is the home page from the user's perspective
    //the UINavController wraps around the MainViewController, or better said, the MainViewController is the root view controller
    MainViewController *vc = [[MainViewController alloc]init];

    UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
    self.navigationController=nc;  // I have a property on the app delegate that references the root view controller, which is my navigation controller.

    [nc release];
    [vc release];

    //show them
    [self.window addSubview:nc.view];
    [self.window makeKeyAndVisible];

    return YES;
}

这篇关于iOS 编程:关于根视图控制器的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆