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

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

问题描述

通过这个问题,我想知道我是否理解Root View控制器的概念。



在iOS应用程序中,根视图控制器控制器,它的视图在启动时添加到UIWindow应用程序,是不是真的?

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

现在,UIWindow也有一个rootViewController属性。当运行前面的代码片段时,该属性是否填充了rvcController或者我必须显式地设置它吗?



然后,在UINavigationController中,



在这种情况下,我第一次添加一个控制器到navigationController堆栈(推送一个新的控制器on),框架是否将该控制器设置为navigationController的RVC,还是必须通过 initWithRootViewController 方法显式地设置它?

解决方案

Ya ..当我开始iPhone dev ..的rootViewController的东西给我一个循环。但它真的很直接。



当应用程序启动时,我在我的应用程序委托类中创建一个UIWindow对象。此外,在该类中,我有一个UIWindow类型的属性,名为window;

   - (BOOL)application:(UIApplication *应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UIWindow * w = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = w;
[w release];
//其他代码在这里...
}

UIViewController 视图将是窗口层次结构中的第一个视图,这可以称为根视图控制器。



令人困惑的部分是...通常我们创建一个 UINavigationController 作为根视图控制器导航控制器有一个init方法,它要求一个RootViewController,这是它将放置在它的堆栈上的第一个viewcontroller。



所以,窗口获得一个控制器,它是 UINavigationController ,它也有一个RootViewController,这是你要显示的第一个视图控制器。



这里是一些代码所有的..(从一个项目中取得)我在我面前打开)

  //调用的应用程序第一次加载和运行..不重新启动该应用程序在内存
- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


//创建基本窗口.. ios thing
UIWindow * w = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = w;
[w release];

//这是从用户视角的主页
// UINavController包装在MainViewController周围,或者更好地说,MainViewController是根视图控制器
MainViewController * vc = [[MainViewController alloc] init];

UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
self.navigationController = nc; //我有一个属性在应用程序委托,引用根视图控制器,这是我的导航控制器。

[nc release];
[vc release];

//显示它们
[self.window addSubview:nc.view];
[self.window makeKeyAndVisible];

return YES;
}


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

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];

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?

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

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?

解决方案

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

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...
}

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

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.

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:有关Root View控制器的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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