编程iOS:关于根视图控制器的说明

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

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

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

现在,一个UIWindow也有一个rootViewController属性。 在运行前面的代码片段时,该属性是否被rvcController填充,还是必须显式设置?

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

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

雅..当我开始iPhone开发.. rootViewController的东西把我扔了一个循环。 但是这非常简单。

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

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIWindow *w = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; self.window=w; [w release]; // other code here... } 

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

令人困惑的部分是…我们常常创build一个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; } 

现在,一个UIWindow也有一个rootViewController属性。 在运行前面的代码片段时,该属性是否被rvcController填充,还是必须将其设置为“明确”?

你必须明确地设置它,如果你这样做,你可以删除addSubview行,因为当你设置一个根视图控制器的时候会自动处理它。

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

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

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

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

首先你可以在Xcode中创build一个空的项目。 在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; 

}

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

}