objective-c – 编程iOS:关于根视图控制器的说明

通过这个问题,我想知道如果我很好地理解Root View Controller的概念.

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

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

现在,UIWindow还有一个rootViewController属性.当运行上一个代码片段时,该属性是否使用rvcController填充,还是必须明确设置?

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

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

解决方法

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

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

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

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

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

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

所以,窗口得到一个“根视图控制器”,它是UINavigationController,它也有一个RootViewController,它是你想要显示的第一个视图控制器.

一旦你排序,它的一切都有道理..我认为:-)

这里是一些代码,它做到这一切..(取自我在我面前打开的一个项目)

//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;
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...