Programming iOS: clarifications about Root View Controller(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 编程:关于根视图控制器的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 编程:关于根视图控制器的说明
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01