ios 使用 AppDelegate 中的 Storyboard 以编程方式实例化视图控制器

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

Instantiating a View Controller programmatically with Storyboard from AppDelegate

iosobjective-cuistoryboard

提问by Robert J. Clegg

I'm busy building an app - that when launched for the first time it asks the user to do two things:

我正在忙于构建一个应用程序 - 当第一次启动时,它要求用户做两件事:

  1. Select a Country
  2. Accept T&Cs
  1. 选择一个国家
  2. 接受条款和条件

From there it goes to the home view controller.

从那里它转到主视图控制器。

The problem I am currently facing is pushing the first view controller onto the screen from my app delegate. I'm using storyboards / Xcode 5/ iOS7

我目前面临的问题是将第一个视图控制器从我的应用程序委托推送到屏幕上。我正在使用故事板/Xcode 5/ iOS7

Here is the code I came up with:

这是我想出的代码:

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
BBCounterySettingsViewController *controller = (BBCounterySettingsViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"CountrySettings"];
[navigationController pushViewController:controller animated:NO];

The problem is the app crashes when it hits the last line of code with the following error:

问题是应用程序在遇到最后一行代码时崩溃并出现以下错误:

*Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController pushViewController:animated:]: unrecognized selector sent to instance 0x8e9a400'

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIViewController pushViewController:animated:]: 无法识别的选择器发送到实例 0x8e9a400”

Anyone have any ideas what I am doing wrong?

任何人都知道我做错了什么?

采纳答案by Rick

You're expecting the self.window.rootViewControllerto be a UINavigationControllerbut it's a UIViewController. This means that the outermost view controller in your storyboard is of the wrong type.

您期望的self.window.rootViewController是 aUINavigationController但它是UIViewController. 这意味着故事板中最外面的视图控制器类型错误。

A better way to obtain the UINavigationController(Which should work in this case) is to use the property self.navigationControlleron any UIViewController.

获得UINavigationController(在这种情况下应该有效)的更好方法是self.navigationController在 any 上使用该属性UIViewController

From what I understand you want to present a view the first time the user runs to have the user pick some stuff. What you should then do is present a modal view controller, like this:

据我了解,您希望在用户第一次运行时展示一个视图,让用户选择一些东西。然后你应该做的是呈现一个模态视图控制器,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//isFirstRun is some boolean you use to determine if it's the first run
if(isFirstRun){
    BBCounterySettingsViewController *controller = (BBCounterySettingsViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: @"CountrySettings"];
    [self.window.rootViewController presentViewController: controller animated:YES completion:nil];
}

回答by Nattudurai

There are two things you need to do:

您需要做两件事:

  1. In Storyboard you have mentioned the controller name (eg LoginView) and enable use storyboard ID
  2. Then you have user the below line

    loginView = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
    [(UINavigationController*)self.window.rootViewController pushViewController:loginView animated:NO];
    
  1. 在 Storyboard 中,您已经提到了控制器名称(例如 LoginView)并启用了使用 storyboard ID
  2. 然后你有用户下面的行

    loginView = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
    [(UINavigationController*)self.window.rootViewController pushViewController:loginView animated:NO];
    

Hope this helps. Let me know if you're still having the issue.

希望这可以帮助。如果您仍然遇到问题,请告诉我。

回答by Gank

[self performSegueWithIdentifier:@"buyListSegue" sender:sender];

回答by regetskcob

I think the controller identifier "CountrySettings"is set to the wrong controller. You're not getting a NavigationControllerwhere you can call pushViewController...

我认为控制器标识符"CountrySettings"设置为错误的控制器。你没有得到一个NavigationController你可以打电话的地方pushViewController......