微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/c/114451.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐