xcode 错误“应用程序窗口应该有一个根视图控制器”(iOS)

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

Error "Application windows are expected to have a root view controller" (iOS)

iosobjective-cxcodeloadviewmopub

提问by Kit Ng

I've created a blank iPhone app project and would like to show a full-screen advertisement during app launch.

我创建了一个空白的 iPhone 应用程序项目,并希望在应用程序启动期间显示全屏广告。

I tried to install the ad by following this guideline: https://github.com/mopub/mopub-ios-sdk/wiki/Interstitial-Integration-For-iOS

我尝试按照以下指南安装广告:https: //github.com/mopub/mopub-ios-sdk/wiki/Interstitial-Integration-For-iOS

That's what I've done finally:

这就是我最后所做的:

In ViewController.h

在 ViewController.h

In ViewController.m

在 ViewController.m 中

Actually all codes are just copied from the previous link.

实际上所有代码都是从上一个链接复制的。

However, an error shows when app runs:

但是,应用程序运行时会显示错误:

Application windows are expected to have a root view controller at the end of application launch

应用程序窗口应该在应用程序启动结束时有一个根视图控制器

I think this error may probably related to the loadView method, because if I remove the loadView method, the error disappeared.

我认为这个错误可能与loadView方法有关,因为如果我删除loadView方法,错误就消失了。

In fact, this error seems common as it can be easily searched on the internet, but I don't know how loadView is related to it, and how can it be solved in my case.

事实上,这个错误似乎很常见,因为它可以在互联网上轻松搜索到,但我不知道 loadView 与它有什么关系,以及如何解决我的情况。

Any solutions? Thanks a lot.

任何解决方案?非常感谢。

回答by Matt Tang

You probably need to do this:

你可能需要这样做:

Add

添加

#import "ViewController.h" 

to the top of AppDelegate.m

到 AppDelegate.m 的顶部

And in AppDelegate.m, your application:didFinishLaunchingWithOptions: method should have some code like this.

在 AppDelegate.m 中,您的 application:didFinishLaunchingWithOptions: 方法应该有一些这样的代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ... Other code

    // Override point for customization after application launch.
    ViewController *viewController = [[ViewController alloc] init];

    self.window.rootViewController = viewController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

回答by Ravi Nadendla

UIViewController *vc = [[UIViewController alloc] init];

 [vc.view addSubview:self.tab_controller.view];

 [self.window setRootViewController:vc];

OR

或者

UIViewController *vc = [[UIViewController alloc] init];

 [vc.view addSubview:yourClass.view];

 [self.window setRootViewController:vc];

回答by DHEERAJ

If you started with an empty template and added a storyboard, you need to do a couple of things:

如果您从一个空模板开始并添加了一个故事板,您需要做几件事:

You need to delete all the lines (except return statement) inside didFinishLaunchingWithOptions

您需要删除 didFinishLaunchingWithOptions 中的所有行(return 语句除外)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        return YES;
}

In project settings ->General, select your storyboard as the main interface

在项目设置->常规中,选择你的storyboard作为主界面

回答by Sumit Kumar Saha

Attached snapshot to help youenter image description here

附上截图来帮助你在此处输入图片说明

At the right side check there is one option under attribute inspector which asks to set as "is rootView controller"

在右侧检查属性检查器下有一个选项,要求设置为“是 rootView 控制器”