ios 从 App Delegate 中的 Storyboard 获取初始视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13289158/
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 21:09:13  来源:igfitidea点击:

Getting Initial View Controller from Storyboard inside the App Delegate

iphoneobjective-cioscocoauistoryboard

提问by Sebastian Boldt

I am trying to access the initial viewcontroller of my storyboard. It is a navigation controller which is wired up to a second viewcontroller via a seque. So after my application did finish launching I want to directly show the viewController that is connected through the segue. I tried it out with code like this but it doesn't work ...

我正在尝试访问我的故事板的初始视图控制器。它是一个导航控制器,通过序列连接到第二个视图控制器。所以在我的应用程序完成启动后,我想直接显示通过 segue 连接的 viewController。我用这样的代码试过了,但它不起作用......

UINavigationController *navController = (UINavigationController*)self.window.rootViewController;
[navController.topViewController performSegueWithIdentifier:@"showLoginScreen" sender:self];

What am I doing wrong?

我究竟做错了什么?

4 years later:

4年后

If I look at my question again after 4 years, I honestly have no idea what my real problem was.

如果我在 4 年后再次看我的问题,老实说,我不知道我真正的问题是什么。

回答by AppleDelegate

Be sure you have tick marked the

确保您已勾选标记

is initial ViewController

是初始的 ViewController

option for UINavigationControllerin storyBoard

UINavigationController故事板中的选项

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UINavigationController * myStoryBoardInitialViewController = [storyboard instantiateInitialViewController];

回答by virtualnobi

Sebastian, I'm not sure why you would want to start the initial view controller manually - all my projects (which all use storyboards) do this automatically if you ticked "Use Storyboards" on the second screen in the "New Project" wizard.

塞巴斯蒂安,我不确定您为什么要手动启动初始视图控制器 - 如果您在“新建项目”向导的第二个屏幕上勾选“使用故事板”,我所有的项目(都使用故事板)会自动执行此操作。

Maybe you need to mark the storyboard scene as inital? This can be done in the scene's attribute inspector by ticking the "Is Initial View Controller" - rather obviously named.

也许您需要将故事板场景标记为初始?这可以在场景的属性检查器中通过勾选“是初始视图控制器”来完成 - 很明显地命名。

And then - if you really have a unusual setup which requires you to access the scenes manually, you can use the following:

然后 - 如果你真的有一个不寻常的设置,需要你手动访问场景,你可以使用以下内容:

UIStoryboard *sb = [UIStoryboard storyboardWithName: "StoryboardName"
                                             bundle: [NSBundle mainBundle]];
UIViewController *vc = [sb instantiateInitialViewController];

(Beware - no code completion here, so check spelling again.)

(注意 - 这里没有代码完成,所以再次检查拼写。)

Or maybe I am getting your question completely wrong..? Happy hacking!

或者,也许我的问题完全错了..?快乐黑客!

回答by Pitono

In my app i have tabBar

在我的应用程序中,我有 tabBar

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

TabBarController *tabBar = [storyBoard instantiateViewControllerWithIdentifier:@"TabBarController"];

_window.rootViewController = tabBar;