Applications are expected to have a root view cont

2018-12-31 08:57发布

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:didFinishLaunchWithOptions method:

- (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.

Anyone know how to fix this issue?

30条回答
只靠听说
2楼-- · 2018-12-31 09:29

Try to connect IBOutlet of tab bar controller to root view in the Interface Builder instead of

self.window.rootViewController = self.tabBarController;

But actually I haven't seen such error before.

查看更多
回忆,回不去的记忆
3楼-- · 2018-12-31 09:30

I had the same problem. If you're building a window-based application "from scratch" as I was, you'll need to do the following: (note, these are steps for Xcode 4.2.)

0. Make sure your application delegate conforms to the UIApplicationDelegate protocol.

For example, suppose our delegate is called MyAppDelegate. In MyAppDelegate.h, we should have something like this:

@interface MyAppDelegate : 
    NSObject <UIApplicationDelegate> // etc...

1. Specify the application delegate in main.m

For example,

#import "MyAppDelegate.h"

int main(int argc, char *argv[])
{
  @autoreleasepool {
    return UIApplicationMain(argc, argv,
      nil, NSStringFromClass([MyAppDelegate class]));
  }
}

2. Create a main window interface file.

To do this, right-click on your project and choose New File. From there, choose Window from the iOS -> User Interface section.

After adding the file to your project, go to the project's summary (left-click on the project; click summary.) Under iPhone/iPod Deployment Info (and the corresponding iPad section if you like) and select your new interface file in the "Main Interface" combo box.

3. Hook it all up in the interface editor

Select your interface file in the files list to bring up the interface editor.

Make sure the Utilities pane is open.

Add a new Object by dragging an Object from the Objects list in the Utilities pane to the space above of below your Window object. Select the object. Click on the Identity inspector in the Utilities pane. Change the Class to the application's delegate (MyAppDelegate, in this example.)

Bring up the connections inspector for MyAppDelegate. Connect the window outlet to the Window that already exists in the interface file.

Click on File's Owner on the left, and then click on the Identity inspector in the Utilities pane. Change the Class to UIApplication

Bring up the connections inspector for File's Owner. Connect the delegate outlet to the MyAppDelegate object.

4. Finally, and very importantly, click on the Window object in the interface file. Open the Attributes inspector. Make sure "Visible at Launch" is checked.

That's all I had to do to get it working for me. Good luck!

查看更多
余欢
4楼-- · 2018-12-31 09:31

If you use MTStatusBarOverlay, then you'll get this error.

MTStatusBarOverlay creates an additional window ([[UIApplication sharedApplication] windows) which doesn't have a root controller.

This doesn't seem to cause a problem.

查看更多
春风洒进眼中
5楼-- · 2018-12-31 09:32

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

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;
    }
}
查看更多
孤独总比滥情好
6楼-- · 2018-12-31 09:32

OrdoDei gave a correct and valuable answer. I'm adding this answer only to give an example of a didFinishLaunchingWithOptions method that uses his answer as well as accounting for the others’ comments regarding Navigation Controller.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    // Instantiate the main menu view controller (UITableView with menu items).
    // Pass that view controller to the nav controller as the root of the nav stack.
    // This nav stack drives our *entire* app.
    UIViewController *viewController = [[XMMainMenuTableViewController alloc] init];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

    // Instantiate the app's window. Then get the nav controller's view into that window, and onto the screen.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // [self.window addSubview:self.navigationController.view];
    // The disabled line above was replaced by line below. Fixed Apple's complaint in log: Application windows are expected to have a root view controller at the end of application launch
    [self.window setRootViewController:self.navigationController];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
查看更多
刘海飞了
7楼-- · 2018-12-31 09:33

This occurred for me because i inadvertently commented out:

[self.window makeKeyAndVisible];

from

- (BOOL)application:(UIApplication*) didFinishLaunchingWithOptions:(NSDictionary*)
查看更多
登录 后发表回答