9

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

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

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

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

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

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

4

4 回答 4

20

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

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

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

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

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

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

因此,窗口获得了一个“根视图控制器”,即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;
}
于 2011-04-30T14:12:17.283 回答
8

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

您必须明确设置它,如果这样做,您可以删除该addSubview行,因为在设置根视图控制器时会自动处理。

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

当然,导航控制器的根视图控制器与窗口的根视图控制器无关。

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

initWithRootViewController 只是初始化空导航控制器并将第一个(根)视图控制器推入堆栈的快捷方式。请注意,这rootViewController不是 的属性UINavigationController,您可以通过 访问它[navController.viewControllers objectAtIndex:0]

于 2011-04-30T14:06:14.393 回答
1

首先你可以在 Xcode 中创建一个空项目。在使用 xiv 在objectivec 类视图控制器上添加新文件之后。现在您可以在 appdeligate.m 中添加此代码并在 appdeligate 中设置 rootviewcontroller

注意:- ViewController.h 导入到 appdeligate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.
   self.window.backgroundColor = [UIColor whiteColor];

   ViewController *viewcontroller =[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];

self.window.rootViewController= viewcontroller;




 [self.window makeKeyAndVisible];
 return YES;

}

于 2014-04-29T05:57:01.243 回答
0
 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.
   self.window.backgroundColor = [UIColor whiteColor];

   ViewController *viewcontroller =[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];

self.window.rootViewController= viewcontroller;




 [self.window makeKeyAndVisible];
 return YES;

}

于 2014-04-29T05:40:53.903 回答