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

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

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

iosobjective-c

提问by ArtSabintsev

I get the following error in my console:

我在控制台中收到以下错误:

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

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

Below is my application:didFinishLaunchWithOptionsmethod:

下面是我的application:didFinishLaunchWithOptions方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Set Background Color/Pattern
    self.window.backgroundColor = [UIColor blackColor];
    self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
    //self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"testbg.png"]];

    // Set StatusBar Color
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];

    // Add the tab bar controller's current view as a subview of the window
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

In Interface Builder, the UITabBarController's delegate is hooked up to the App Delegate.

在 Interface Builder 中,UITabBarController的委托连接到应用程序委托。

Anyone know how to fix this issue?

有谁知道如何解决这个问题?

采纳答案by sho

I had this same problem. Check your main.m. The last argument should be set to the name of the class that implements the UIApplicationDelegate protocol.

我有同样的问题。检查您的 main.m。最后一个参数应设置为实现 UIApplicationDelegate 协议的类的名称。

retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");

回答by OrdoDei

Replace in AppDelegate

在 AppDelegate 中替换

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];

回答by Warkst

I had the same error when trying to change the first view controller that was loaded in

尝试更改加载的第一个视图控制器时遇到了同样的错误

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

At first I didn't really know where the error was coming from precisely so I narrowed it down and found out what went wrong. It turns out that I was trying to change the display of a view before it actually came on screen. The solution was hence to move this code in the viewcontroller that was giving me trouble from

起初我真的不知道错误究竟来自哪里,所以我缩小了范围并找出出了什么问题。事实证明,我试图在视图实际出现在屏幕上之前更改它的显示。因此,解决方案是将这段代码移动到给我带来麻烦的视图控制器中

- (void)viewDidLoad

- (void)viewDidLoad

to

- (void)viewDidAppear:(BOOL)animated

- (void)viewDidAppear:(BOOL)animated

and the error stopped appearing. My problem specifically was caused by making a UIAlertViewshow.

并且错误停止出现。我的问题特别是由制作UIAlertView节目引起的。

In your case I suggest you check out the code in the tabBarController's active view controller (as it is probably a problem in that view controller). If that doesn't work, try to set the starting settings in the nib file instead of in code - or if you want to do it in code, try moving the code to the tabBarController's active viewcontroller's appropriate method.

在您的情况下,我建议您查看 tabBarController 的活动视图控制器中的代码(因为它可能是该视图控制器中的问题)。如果这不起作用,请尝试在 nib 文件中而不是在代码中设置起始设置 - 或者如果您想在代码中进行设置,请尝试将代码移动到 tabBarController 的活动视图控制器的适当方法。

Good luck!

祝你好运!

回答by jlujan

I got this when starting with the "Empty Application" template and then manually adding a XIB. I solved it by setting the main Nib name as suggested by Sunny. The missing step in this scenario is removing

我在从“空应用程序”模板开始然后手动添加 XIB 时得到了这个。我按照 Sunny 的建议通过设置主要 Nib 名称解决了这个问题。此场景中缺少的步骤是删除

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

from

application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

As it will overwrite the instance of your window created in the Xib file. This is assuming you have created a ViewController and wired it up with your window and App Delegate in the XIB file as well.

因为它会覆盖在 Xib 文件中创建的窗口实例。这是假设您已经创建了一个 ViewController 并将其与您的窗口和 XIB 文件中的 App Delegate 连接起来。

回答by arindam

This happened to me. Solved by editing .plist file. Specify the Main nib file base name.(Should be MainWindow.xib). Hope this will help.

这发生在我身上。通过编辑 .plist 文件解决。指定 Main nib 文件基本名称。(应该是 MainWindow.xib)。希望这会有所帮助。

enter image description here

在此处输入图片说明

回答by denicija

I run into the same problem recently, when building a project with ios5 sdk. At first it was building and running properly, but after that the error appeared.
In my case the solution was rather simple.
What was missing, was that somehow the Main Interface property in the summary tab of my application target got erased. So I needed to set it again.

我最近在使用 ios5 sdk 构建项目时遇到了同样的问题。起初它正在构建和运行正常,但之后出现错误。
就我而言,解决方案相当简单。
缺少的是,不知何故,我的应用程序目标的摘要选项卡中的主界面属性被删除了。所以我需要重新设置它。


If this is not the point, and if the tabBarController is still nil, you can always programmatically create your window and root controller. As a fallback I added the following code to my project


如果这不是重点,并且 tabBarController 仍然为零,则您始终可以通过编程方式创建窗口和根控制器。作为后备,我将以下代码添加到我的项目中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    if (!window && !navigationController) {
        NSLog(@"Window and navigation controller not loaded from nib. Will be created programatically.");
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        UIViewController *viewController1, *viewController2;
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];

        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
        self.window.rootViewController = self.tabBarController;

    }
    else {
        [window addSubview:[tabBarController view]];
    }
    [self.window makeKeyAndVisible];
    return YES;
}

This will work only if sho's solution is implemented also.

仅当 sho 的解决方案也被实施时,这才有效。

回答by Mike Flynn

I upgraded to iOS9 and started getting this error out of nowhere. I was able to fix it but adding the below code to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

我升级到 iOS9 并开始莫名其妙地出现这个错误。我能够修复它,但将以下代码添加到- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *window in windows) {
    if(window.rootViewController == nil){
        UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
        window.rootViewController = vc;
    }
}

回答by RyeMAC3

None of the above suggestions solved my problem. Mine was this:

以上建议都没有解决我的问题。我的是这样的:

Add:

添加:

window.rootViewController = navigationController;

after:

后:

[window addSubview:navigationController.view];

in my appdelegate's

在我的 appdelegate 中

- (void)applicationDidFinishLaunching:(UIApplication *)application {

回答by bearMountain

  • Select your "Window" in your Nib File
  • In "Attributes Inspector" Check "Visible at Launch"
  • 在 Nib 文件中选择“窗口”
  • 在“属性检查器”中勾选“启动时可见”

image![]

图片![]

  • This happens when your nib file is created manually.
  • This fix works for regular nib mode - not storyboard mode
  • 当您手动创建 nib 文件时会发生这种情况。
  • 此修复适用于常规笔尖模式 - 不适用于故事板模式

回答by rémy

how to add a RootViewController for iOS5

如何为 iOS5 添加 RootViewController

if your app didn't use a RootViewController till now, just create one ;) by hitting File > New > New File; select UIViewController subclassname it RootViewController, uncheck the With XIB for user interface (assuming you already have one) and put this code in your AppDelegate :: didFinishLaunchingWithOptions

如果您的应用程序到现在为止还没有使用 RootViewController,只需创建一个 ;) 通过点击 File > New > New File; 选择将其UIViewController subclass命名为RootViewController,取消选中 With XIB for user interface(假设您已经有一个)并将此代码放入您的 AppDelegate :: didFinishLaunchingWithOptions

rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;

for sure - you have to import RootViewController.h and create the variable

当然 - 你必须导入 RootViewController.h 并创建变量

here is a nice articleabout the RootViewController and the AppDelegate,

这是一篇关于 RootViewController 和 AppDelegate的好文章