Application windows are expected to have a root view controller at the end of application launch - even with all known issues fixed

拜拜、爱过 提交于 2019-12-28 04:19:32

问题


I have this problem, however none of the information I can find on this forum or the internet in general seems to be able to help me.

There seem to be two places where this error can come about:

  1. main.m - my function looks like this:
  int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }

The last argument in UIApplicationMain returns an NSString value of the class of my AppDelegate. This is therefore working fine.

2.AppDelegate.m - there is an "older" way of setting the root view controller which is like this:

  [self.window addSubview:rootViewController];

However, in my app it has already been updated to:

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

So none of the current information on the internet works. It is slightly more puzzling as my colleague can get it to work on his computer perfectly fine - he was the one that sent me the app source code so all the settings and code should be exactly the same.

I am trying to launch this in the simulator. It is built against iOS 5, but I'm trying to run it on the iOS 6.0 simulator.

I have the latest XCode (4.5.1).

Is there any reason this would be happening? And how can I rectify it?

Many thanks

Tom


回答1:


I ran into exactly the same thing trying to add a UITableView to a single-view app. Instead, create a default Master-Detail Application project (file->new->target->...) and see the AppDelegate's implementation of didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

MDMasterViewController *masterViewController = [[MDMasterViewController alloc] initWithNibName:@"MDMasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}

Rather than directly setting your view controller as the window's rootViewController, you need to create a navigation controller init'ed with your view controller for initWithRootViewController, then set that nav controller as the window's rootViewController. (Notice you also have to squirrel away that nav controller in a property so it doesn't get destructed).




回答2:


Just change this:

[window addSubview:tabBarController.view];

to this:

[window setRootViewController:tabBarController];

Or whatever was in the addSubView:




回答3:


Try to define the default view controller in your project menu,

select your project => Summary => Main Interface => Type your main view controller

every time that i started new project i faced the same error as you,doing this every time solved,hope this help you.




回答4:


Do the following add an Exception BreakPoint on your code at least to see if you can figure out what's the problem. Use this tutorial if you don't know how to do it. Also, are there any warnings when you compile? Even if it let's you compile, sometimes warnings can reveal why your app is crashing.



来源:https://stackoverflow.com/questions/12784411/application-windows-are-expected-to-have-a-root-view-controller-at-the-end-of-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!