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

后端 未结 30 1304
野的像风
野的像风 2020-11-22 06:42

I get the following error in my console:

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

相关标签:
30条回答
  • 2020-11-22 07:30

    This issue happens when you don't have Interface Builder set up correctly.

    Ensure your App Delegate's window and viewController outlets are hooked up:

    In your MainWindow.xib, hold control, click App Delegate and drag to the Window object. Select window. Hold control and select the App delegate again, drag to your root view controller and select viewController.

    0 讨论(0)
  • 2020-11-22 07:32

    how to add a RootViewController for iOS5

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

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

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

    here is a nice article about the RootViewController and the AppDelegate,

    0 讨论(0)
  • 2020-11-22 07:34

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

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

    to

    - (void)viewDidAppear:(BOOL)animated

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

    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.

    Good luck!

    0 讨论(0)
  • 2020-11-22 07:34

    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!

    0 讨论(0)
  • 2020-11-22 07:35

    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.


    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

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

    0 讨论(0)
  • 2020-11-22 07:35

    Received the same error after replacing my UI with a Storyboard, using XCode 4.6.3 and iOS 6.1

    Solved it by clearing out all of the code from didFinishLaucnhingWithOptions in the AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        return YES;
    }
    
    0 讨论(0)
提交回复
热议问题