xcode 如何在“基于导航的应用程序”中替换 RootViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5827613/
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
How to replace RootViewController in "Navigation-based application"
提问by iter
I have an application that uses the "navigation-based application" template in XCode.
我有一个在 XCode 中使用“基于导航的应用程序”模板的应用程序。
Now I want to change it so that the first view that loads is a regular (custom) UIView, and if the user clicks a particular button, I push the original RootViewController onto the NavigationController.
现在我想更改它,以便加载的第一个视图是常规(自定义)UIView,如果用户单击特定按钮,我会将原始 RootViewController 推送到 NavigationController。
I understand that somewhere, someone is calling this with my RootViewController:
我知道在某个地方,有人用我的 RootViewController 调用它:
- (id)initWithRootViewController:(UIViewController *)rootViewController
I want to know how to replace the argument with my new class.
我想知道如何用我的新课程替换参数。
回答by saadnib
if you want to replace the root view controller of your navigation stack you can replace the first object of its view controllers array as -
如果要替换导航堆栈的根视图控制器,可以将其视图控制器数组的第一个对象替换为 -
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
NewViewController *nvc = [[NewViewController alloc] init];
[viewControllers replaceObjectAtIndex:0 withObject:nvc];
[self.navigationController setViewControllers:viewControllers];
回答by woody121
^ These are all ways to do it programmatically. Thats cool. But I use the interface builder and storyboards in Xcode, so this is the easy and fast way to add a new view controller:
^ 这些都是以编程方式完成的方法。这很酷。但是我在 Xcode 中使用了界面构建器和故事板,所以这是添加新视图控制器的简单快捷的方法:
- Open the storyboard in question
- Add a new view controller to your storyboard by dragging it from the objects list (right hand tool bar bottom)
- While holding down the CONTROL key, click and drag from the middle of your navigation controller (should be blank and gray) to your new fresh white view.
- On the popup, selection Relation Segue: Root View Controller (should be below the normal push/modal/custom options you have likely seen before)
- 打开有问题的故事板
- 通过从对象列表(右侧工具栏底部)中拖动它,将一个新的视图控制器添加到您的故事板
- 在按住 CONTROL 键的同时,单击并从导航控制器的中间(应该是空白和灰色)拖动到新的白色视图。
- 在弹出窗口中,选择 Relation Segue: Root View Controller(应该低于您之前可能见过的正常推送/模式/自定义选项)
Tada! Enjoy your new root view controller without holding your day up with programmatic creation.
多田!享受您的新根视图控制器,而无需通过程序化创建来拖延您的一天。
回答by ljkyser
Look inside the main app delegate .m file and find the method
查看主应用程序委托 .m 文件并找到方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Inside it will be a line like this
里面会有这样的一行
self.window.rootViewController = self.navigationController;
self.window.rootViewController = self.navigationController;
You can instantiate a diffent view controller there and assign it to be the rootViewController
您可以在那里实例化一个不同的视图控制器并将其分配为 rootViewController