iOS 7 - 无法实例化默认视图控制器

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

iOS 7 - Failing to instantiate default view controller

iosiphoneobjective-cxcodexcode5

提问by rodrigoalvesvieira

I am using Xcode 5 in a newly created app and when I just create it I go for the run button e click on it, then the project gets built but it does not show in the iOS Simulator and I get the following message:

我在一个新创建的应用程序中使用 Xcode 5,当我刚刚创建它时,我点击运行按钮,然后点击它,然后项目被构建,但它没有显示在 iOS 模拟器中,我收到以下消息:

 Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - 
 perhaps the designated entry point is not set?

I've Googled about it of course and everybody points out that this is happening because XCode does not know yet which view controller is the initial one. But the weird thing is that I created the app as a page based (also tried single-view and tabbed app options) app and XCode already had defined a Storyboard for it.

我当然已经在谷歌上搜索过了,每个人都指出这是因为 XCode 还不知道哪个视图控制器是最初的。但奇怪的是,我将应用程序创建为基于页面的(也尝试过单视图和选项卡式应用程序选项)应用程序,而 XCode 已经为它定义了一个故事板。

Also, when I go to the main interface option of the project the storyboard (named "Main" by Xcode itself) is set, and in the Storyboard, my view controller is set as the "Initial View Controller"

此外,当我转到项目的主界面选项时,故事板(由 Xcode 本身命名为“Main”)已设置,并且在故事板中,我的视图控制器被设置为“初始视图控制器”

enter image description here

在此处输入图片说明

What is wrong?

怎么了?

采纳答案by Ryna

So this also happened to me too. I checked 50 times and my "Is Initial View Controller" was checked, believe me. It happened out of the blue. So how did I fix it?

所以这也发生在我身上。我检查了 50 次并且检查了我的“是初始视图控制器”,相信我。它突然发生了。那我是怎么修复的呢?

  1. Create a new Storyboard in your project, name it something like Main_iPhoneV2 (or iPadV2 depending on your original storyboard style)
  2. Open the broken storyboard, click anywhere in the white area and press command-a, then command-c (select all and copy)
  3. Open your new storyboard and press command-v to paste the same exact setup
  4. Go to your project settings, change your "Main Interface" to the new Main_iPhoneV2 (If it's your iPad and you're writing a universal app, you'll have to edit the -Info.plist and look for the value "Main storyboard file base name (iPad)
  5. Recompile, and stop pulling your hair out
  1. 在您的项目中创建一个新的故事板,将其命名为 Main_iPhoneV2(或 iPadV2,具体取决于您的原始故事板样式)
  2. 打开坏掉的故事板,点击白色区域的任意位置并按command-a,然后command-c(全选并复制)
  3. 打开您的新故事板并按 command-v 粘贴相同的确切设置
  4. 转到您的项目设置,将您的“主界面”更改为新的 Main_iPhoneV2(如果是您的 iPad 并且您正在编写一个通用应用程序,则必须编辑 -Info.plist 并查找值“主故事板文件”基本名称 (iPad)
  5. 重新编译,不要再拔头发了

回答by Ashvin Ajadiya

Check Is Initial View Controllerin the Attributes Inspector.

在属性检查器中检查是初始视图控制器

enter image description here

在此处输入图片说明

回答by Andy

First click on the View Controller in the right hand side Utilitiesbar. Next select the Attributes Inspectorand make sure that under the View Controller section the 'Is Initial View Controller'checkbox is checked!

首先单击右侧实用工具栏中的视图控制器。接下来选择Attributes Inspector并确保在 View Controller 部分下选中了“Is Initial View Controller”复选框!

回答by superarts.org

This warning is also reported if you have some code like:

如果您有以下代码,也会报告此警告:

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.rootViewController = myAwesomeRootViewController
    window?.makeKeyAndVisible()

In this case, go to the first page of target settings and set Main Interfaceto empty, since you don't need a storyboard entry for your app.

在这种情况下,转到目标设置的第一页并将其设置Main Interface为空,因为您的应用程序不需要故事板条目。

回答by Jayprakash Dubey

Using Interface Builder :

使用界面生成器:

Check if 'Is initial view controller' is set. You can set it using below steps :

检查是否设置了“是初始视图控制器”。您可以使用以下步骤进行设置:

  1. Select your view controller(which is to be appeared as initial screen).
  2. Select Attribute inspectorfrom Utilitieswindow.
  3. Select 'Is Initial View Controller' from View Controllersection (if not).
  1. 选择您的视图控制器(将作为初始屏幕出现)。
  2. 实用程序窗口中选择属性检查器。
  3. 从“视图控制器”部分选择“是初始视图控制器” (如果不是)。

If you have done this step and still getting error then uncheck and do it again.

如果您已完成此步骤但仍出现错误,则 uncheck and do it again.

Steps to solve problem

Steps to solve problem

Using programmatically :

以编程方式使用:

Objective-C :

目标-C:

        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; // <storyboard id>

        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];

        return YES;

Swift :

斯威夫特:

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        var objMainViewController: MainViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MainController") as! MainViewController

        self.window?.rootViewController = objMainViewController

        self.window?.makeKeyAndVisible()

        return true

回答by Cons Bulaquena

I have experienced this with my Tab Bar Controller not appearing in the Simulator along with a black screen. I did the following in order for my app to appear in the Simulator.

我的 Tab Bar Controller 没有出现在模拟器中并出现黑屏时,我遇到过这种情况。为了让我的应用程序出现在模拟器中,我执行了以下操作。

  1. Go to Main.storyboard.
  2. Check the Is Initial View Controllerunder the Attributes inspector tab.
  1. 转到 Main.storyboard。
  2. 检查Is Initial View Controller属性检查器选项卡下的。

Is Initial View Controller in the Attributes Inspector.

Is Initial View Controller in the Attributes Inspector.

回答by Shamsudheen TK

Setup the window manually,

手动设置窗口,

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if (!application.keyWindow.rootViewController) 
     {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

        UIViewController *myViewController= [storyboard instantiateViewControllerWithIdentifier:@"myViewController identifier"];

         application.keyWindow.rootViewController = myViewController;
     }
}

回答by Hovanes Mosoyan

I get this error when I change the the storyboard file name "Main.storyboard" TO: "XXX.storyboard"

当我将故事板文件名“ Main.storyboard”更改为:“ XXX.storyboard”时出现此错误

The solution for me was:

我的解决方案是:

  • Product->Clean
  • CHANGE: Supporting Files-> info.plist-> Main storyboard file base name-> MainTO: XXX
  • 产品->清洁
  • 更改:支持文件-> info.plist->主故事板文件基本名称->主要文件:XXX

Good Luck

祝你好运

回答by Walter Schurter

Product "Clean" was the solution for me.

产品“清洁”是我的解决方案。

回答by Kashif Ahmed

1st option

第一个选项

if you want to set your custom storyboard instead of a default view controller.

如果您想设置自定义故事板而不是默认视图控制器。

Change this attribute from info.plist file

从 info.plist 文件更改此属性

<key>UISceneStoryboardFile</key> <string>Onboarding</string>

<key>UISceneStoryboardFile</key> <string>Onboarding</string>

Onboardingwould be your storyboard name

入职将是您的故事板名称

to open this right-click on info.plist file and open as a source code

打开这个右键单击 info.plist 文件并作为源代码打开

2nd option

第二个选项

1- Click on your project

1- 单击您的项目

2- Select your project from the target section

2- 从目标部分选择您的项目

3- Move to Deployment interfacesection

3- 移至部署界面部分

4- Change your storyboard section from Main Interfacefield

4- 从主界面字段更改您的故事板部分

Please remember set your storyboard initial view controller

请记住设置您的故事板初始视图控制器