需要帮助来了解UIViewController和UIWindow初始化 [英] Need help understanding UIViewController and UIWindow initialization
问题描述
我想知道是否有人可以解释下面的代码块,因为我不太了解.
I was wondering if anyone could explain the following block of code because I don't really understand it.
self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
然后,当您要展示新的vc时,您可以执行以下操作:
Then when you want to present a new vc you can do this:
OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
[ self.navigationController pushViewController:ovc animated:YES ];
要返回,请执行以下操作:
To go back do this:
[ self.navigationController popViewControllerAnimated:YES ];
推荐答案
导航控制器需要一个根"视图控制器,这是它管理的视图控制器堆栈中的底部视图控制器.
A navigation controller needs a "root" view controller, which is the bottom view controller in the stack of view controllers it manages.
#1 self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
#2 self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
#3 self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
#4 [self.window makeKeyAndVisible];
第1行创建一个类"RootViewController"(必须是自定义视图控制器类)的视图控制器.它从同名的nibfile加载视图控制器的视图.这类似于使用InstantiateViewControllerWithIdentifier从情节提要中加载视图控制器,不同之处在于,您必须指定要创建的视图控制器的类以及正在加载
Line 1 creates a view controller of class "RootViewController" (which must be a custom view controller class.) It loads the view controller's views from a nibfile of the same name. This is similar to using instantiateViewControllerWithIdentifier to load a view controller from a storyboard, except that you have to specify the class of view controller you're creating, and the nibfile you're loading the
第2行使用新创建的"RootViewController"作为根视图控制器来创建导航控制器
Line 2 creates a navigation controller with the newly created "RootViewController" as it's root view controller
第3行将导航控制器安装为应用程序窗口的根视图控制器.
Line 3 installs the navigation controller as the root view controller of the application window.
第4行使应用程序窗口成为活动窗口.
Line 4 makes the app window the active window.
这篇关于需要帮助来了解UIViewController和UIWindow初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!