10

I am getting a message within the console when I run my app that says:

2011-11-16 19:17:41.292 Juice[8674:707] Applications are expected to have a root view controller at the end of application launch

I have heard from others that this has to do with the method didFinishLaunchingWithOptions

If anyone has any suggestions for why I am getting this error, it would be much appreciated.

My code for the method:

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

    // Override point for customization after application launch.

    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

    return YES;
}
chown
  • 51,908
  • 16
  • 134
  • 170
Teddy13
  • 3,824
  • 11
  • 42
  • 69
  • 4
    possible duplicate of [Applications are expected to have a root view controller at the end of application launch](http://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati) – Justin Spahr-Summers Nov 28 '11 at 21:15
  • did you solve this problem? if you did, can you please post the solution? – Hosni Jun 05 '12 at 09:01
  • Other dupes: http://stackoverflow.com/q/12784411/9530 http://stackoverflow.com/q/8706828/9530 http://stackoverflow.com/q/11515818/9530 http://stackoverflow.com/q/9844626/9530 and maybe more – Adam Rosenfield Mar 08 '13 at 20:41

15 Answers15

54

You should replace the

[window addSubview:tabBarController.view];

to

[self.window setRootViewController:tabBarController];

Maybe you built your project with 'Empty Application' and forgot to set the rootViewController in your didFinishLaunchingWithOptions (which exists in your AppDelegate.m).

However, if you build your project with 'Single View Application' or some other type, the project will set the rootViewController via xib by default (which might be a MainWindow.xib in your project).

Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • 1
    On my second day trying to fix an orientation issue and then noticing the same message in my console output... adding this adjustment fixed everything and orientation now works like it should. For some reason it kept loading in a few of the iPad xibs in my universal app in portrait (when the project settings had only landscape orientation as a possibility). Thanks. – Wollan Oct 23 '12 at 08:00
  • How do I set the `rootViewController`? – powtac Jun 25 '13 at 10:55
  • @powtac , there're many examples available for it, you can take a look at this one: https://github.com/Kjuly/KYCircleMenu/blob/dev/KYCircleMenuDemo/KYCircleMenuDemo/AppDelegate.m – Kjuly Jun 26 '13 at 00:47
13

I had the same problem on iOS 5, after adding a storyboard to an "empty" project. It turns out I had to remove all the lines in AppDelegate.m that set values to self.window.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    //self.window.backgroundColor = [UIColor whiteColor];
    //[self.window makeKeyAndVisible];
    return YES;
}
Frank
  • 2,375
  • 1
  • 19
  • 17
6

If you have MainWindow.xib, make sure you set Main Interface in Target's summary to MainWindow.

anja
  • 61
  • 2
4

The way I got this error Applications are expected to have a root view controller at the end of application launch to disappear, was to ensure the loadView method in my root view controller was calling [super loadView]. Hope this helps someone.

coco
  • 2,998
  • 1
  • 35
  • 58
3

Try using self.window instead of window (if your setup has window being synthesized with something like @synthesize window=_window;):

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

    // Override point for customization after application launch.

    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

    return YES;
}

2nd possibility:

In your main.m make sure the last argument is the name of the App Delegate. In your case, it looks like it should be:

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

Solution:

As @marcus13 said in the comments below.. This was fixed was found in this SO answer: Applications are expected to have a root view controller at the end of application launch - by by moving the UIAlertView methods from -(void)viewDidLoad to -(void)viewDidAppear:(BOOL)animated

Community
  • 1
  • 1
chown
  • 51,908
  • 16
  • 134
  • 170
  • Thanks for the quick reply chown. I tried self.window instead of window. No luck. Also did @synthesize window=_window and no luck as well – Teddy13 Nov 19 '11 at 00:06
  • @marcus13 If that doesnt solve it, check http://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati - it has lots of good suggestions to fix this. – chown Nov 19 '11 at 00:11
  • 1
    Your suggestion did not work either. It used to be retVal = UIApplicationMain(argc, argv, nil, nil); and I changed it to what you suggested. I will check out the link. Just wondering if this is enough for the app to get rejected by apple? Everything "works". Thank you again chown, you have been tremendously helpful – Teddy13 Nov 19 '11 at 00:25
  • @marcus13 Yea, Apple will see that warning and probably reject your App =/. When you get it solved can you post it as an answer here so we can see how you fixed it? – chown Nov 19 '11 at 00:27
  • Here is the answer... May it be noted that I found the solution from this post http://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati The solution that worked for me was putting my UIAlertView that was notifying the user there was internet connection in -(void)viewDidAppear:(BOOL)animated instead of -(void)viewDidLoad – Teddy13 Nov 19 '11 at 00:35
2

I just ran into this issue while building a new project from scratch. I added a StoryBoard and build my whole interface, but i did not select a template.

When doing it this way, you have to make sure of 3 main things:

Always select your initial controller (TabBarcontroller or NavigationController) as the initial view in your Storyboard.

Change the code in your Appdelegate.m from this

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

to this

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

Check your [ProjectName]-Info.plist file. If there is no key named "Main storyboard file base name", you have to manually add it and set it's value to the name of your storyboard file (without the extension).

After i did all of these steps, my application ran perfectly.

schaermu
  • 13,478
  • 2
  • 39
  • 32
2

Another cause:

I was in IB attaching File Owner to a new small ImageView I'd dragged onto the View. I hadn't called it an IBOutlet in the .h file, so when I ctrl-dragged to it, the new Imageview wasn't listed as a possible connection. The only possibility displayed in the little black box was View. I must have clicked, inadvertently. I made a few changes then ran the program and got the Root Controller error. The fix was reconnecting File Owner to the bottom View in the xib - IB screen.

Rob Smythe
  • 409
  • 1
  • 3
  • 16
1

I also had the same error while developing an app that uses sqlite Database. I was showing alertView when the db file transfer failed. This was a mistake since you cannot show any popovers/alertview/actions without any rootViewController set!

I fixed it by ensuring that any function that creates and shows these alerts/popovers/actionsheets are called after

self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
[someObject functionthatDisplayAlerts];
shawndreck
  • 63
  • 8
1

I know this post is old but I ran into this today.

It's because I created a UIAlertView in didFinishLaunchingWithOptions.

Assuming then we should not be doing this because I commented it out and the error went away. I removed my comments and the error came back.

The app doesn't crash, I just get that logged error.

Stephan
  • 41,764
  • 65
  • 238
  • 329
Louis
  • 11
  • 1
1

I had the same problem with my App. It appeared when I added another view controller to my project and tried to set it as the root view controller in AppDelegate. I tried several solutions, but none of them could fix the problem. Finally I found the cause: I had two localized versions of the MainWindow.xib file (One for german and another for english localization). So I deleted the english file and reconnected the IBOutlets in MainView.xib. This solved the problem.

blauzahn
  • 351
  • 2
  • 6
1

I'm not sure if this will help anybody else, but if you have used interface builder to create your mainWindow and have done all the linking between the delegate make sure you don't have the following code within application:didFinishLaunching ...

[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]

I was having the same error until I removed the above line. Hope that helps!

EDIT: Doing the above now has my viewControllers viewDidAppear method being called twice ?

Ray A
  • 307
  • 2
  • 14
1

I also had the same problem. All I got was a black screen. Turns out I had inadvertently removed:

[self.window makeKeyAndVisible];

from my code. Hope this helps someone!

capikaw
  • 12,232
  • 2
  • 43
  • 46
1

I had two outlets assigned to "view" in the storyboard's root view controller. Right-click on "view controller" and make sure there's only one "view".

jab
  • 4,053
  • 3
  • 33
  • 40
0

If you are using Storyboard, but created an empty project, you probably forgot to set the Main storyboard to your *.storyboard file in the Summary tab in your project settings. It helped me to solve this problem.

dreamzor
  • 5,795
  • 4
  • 41
  • 61
0

If you are starting from an empty you have to make this addition to your AppDelegate.m file, to "point" the window to the rootViewController (self.window.rootViewController = [[[ViewControllerName alloc] initWithNibName:@"ViewControllerName" bundle:nil] autorelease];)

Like so:

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

{

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

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

self.window.rootViewController = [[[ViewControllerName alloc] initWithNibName:@"ViewControllerName" bundle:nil] autorelease];

[self.window makeKeyAndVisible];

return YES;

}
Kampai
  • 22,848
  • 21
  • 95
  • 95
ptrott
  • 1
  • 1