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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5842087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:47:00  来源:igfitidea点击:

Programming iOS: clarifications about Root View Controller

objective-ccocoa-touchiosios4rootview

提问by Lorenzo B

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?

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

[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?

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

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

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

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 initWithRootViewControllermethod?

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

回答by Jason Cragun

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

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

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;

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

- (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 UIViewControllerwhose viewwill be the first view in the window hierarchy, this could be called the "root view controller".

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

The confusing part is...that often we create a UINavigationControlleras 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.

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

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.

因此,窗口获得了一个“根视图控制器”,即UINavigationController,它也有一个 RootViewController,它是您要显示的第一个视图控制器。

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

回答by omz

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 explicity?

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

You have to set it explicitly, and if you do, you can remove the addSubviewline, because that's handled automatically when you set a root view controller.

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

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

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

Of course, a navigation controller's root view controller has nothing to do with that of the window.

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

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 explicity through initWithRootViewController method?

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

initWithRootViewController is just a shortcut for initializing an empty navigation controller and pushing the first (root) view controller onto the stack. Note that rootViewControlleris not a property of UINavigationController, you would access it via [navController.viewControllers objectAtIndex:0].

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

回答by omz

firstly you can create A empty project in Xcode. after you add the new file on objectivec class view controller with xiv. now you can add to this code in appdeligate.m and set the rootviewcontroller in appdeligate

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

NOTE:- ViewController.h import to the appdeligate.m

注意:- 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;

}

}

回答by omz

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

}

}