Applications are expected to have a root view controller at the end of application launch
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.tabB开发者_开发技巧arController;
[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?
Replace in AppDelegate
[window addSubview:[someController view]];
to
[self.window setRootViewController:someController];
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.
retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
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!
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
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.
This happened to me. Solved by editing .plist file. Specify the Main nib file base name.(Should be MainWindow.xib). Hope this will help.
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.
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;
}
}
None of the above suggestions solved my problem. Mine was this:
Add:
window.rootViewController = navigationController;
after:
[window addSubview:navigationController.view];
in my appdelegate's
- (void)applicationDidFinishLaunching:(UIApplication *)application {
- Select your "Window" in your Nib File
- In "Attributes Inspector" Check "Visible at Launch"
- This happens when your nib file is created manually.
- This fix works for regular nib mode - not storyboard mode
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,
I also had this error but unlike any of the answers previously listed mine was because i had uncommented the method 'loadView' in my newly generated controller (xcode 4.2, ios5).
//Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
It even told me that the method was for creating the view programmatically but i missed it because it looked so similar to other methods like viewDidLoad that i normally use i didn't catch it.
To solve simply remove that method if you are not programmatically creating the view hierarchy aka using nib or storyboard.
i got this problems too. i got my project run in xcode4.2.1. i've read all comments up there, but no one is cool for me. after a while, i find that i commented a piece of code.
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
then i uncommented it. everything is ok for me. hope this would helpful for you guys.
Make sure you have this function in your application delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
return YES;
}
Make sure didFinishLaunchingWithOptions returns YES. If you happened to remove the 'return YES' line, this will cause the error. This error may be especially common with storyboard users.
With my first view being MenuViewController
I added:
MenuViewController *menuViewController = [[MenuViewController alloc]init];
self.window.rootViewController = menuViewController;
on the App Delegate method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}
That worked.
There was a slight change around iOS 5.0 or so, requiring you to have a root view controller. If your code is based off older sample code, such as GLES2Sample, then no root view controller was created in those code samples.
To fix (that GLES2Sample, for instance), right in applicationDidFinishLaunching
, I create a root view controller and attach my glView to it.
- (void) applicationDidFinishLaunching:(UIApplication *)application
{
// To make the 'Application windows are expected
// to have a root view controller
// at the end of application launch' warning go away,
// you should have a rootviewcontroller,
// but this app doesn't have one at all.
window.rootViewController = [[UIViewController alloc] init]; // MAKE ONE
window.rootViewController.view = glView; // MUST SET THIS UP OTHERWISE
// THE ROOTVIEWCONTROLLER SEEMS TO INTERCEPT TOUCH EVENTS
}
That makes the warning go away, and doesn't really affect your app otherwise.
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.
I solved the problem by doing the following (none of the other solutions above helped):
From the pulldown menu associated with "Main Interface" select another entry and then reselect "MainWindow" then rebuild.
I came across the same issue but I was using storyboard
Assigning my storyboard
InitialViewController
to my window's rootViewController
.
In
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
UIStoryboard *stb = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
self.window.rootViewController = [stb instantiateInitialViewController];
return YES;
}
and this solved the issue.
I began having this same issue right after upgrading to Xcode 4.3, and only when starting a project from scratch (i.e. create empty project, then create a UIViewController, and then Create a separate nib file).
After putting ALL the lines I used to, and ensuring I had the correct connections, I kept getting that error, and the nib file I was trying to load through the view controller (which was set as the rootController) never showed in the simulator.
I created a single view template through Xcode and compared it to my code and FINALLY found the problem!
Xcode 4.3 appears to add by default the method -(void)loadView; to the view controller implementation section. After carefully reading the comments inside it, it became clear what the problem was. The comment indicated to override loadView method if creating a view programmatically (and I'm paraphrasing), otherwise NOT to override loadView if using a nib. There was nothing else inside this method, so in affect I was overriding the method (and doing nothing) WHILE using a nib file, which gave the error.
The SOLUTION was to either completely remove the loadView method from the implementation section, or to call the parent method by adding [super loadView].
Removing it would be best if using a NIB file as adding any other code will in effect override it.
I had this same error message in the log. I had a UIAlertView pop up in application:didFinishLaunchingWithOptions. I solved it by delaying the call to the alertView to allow time for the root view controller to finishing loading.
In application:didFinishLaunchingWithOptions:
[self performSelector:@selector(callPopUp) withObject:nil afterDelay:1.0];
which calls after 1 second:
- (void)callPopUp
{
// call UIAlertView
}
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!
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.
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;
}
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;
}
This occurred for me because i inadvertently commented out:
[self.window makeKeyAndVisible];
from
- (BOOL)application:(UIApplication*) didFinishLaunchingWithOptions:(NSDictionary*)
I was able to set the initial view controller on the summary screen of xcode.
Click on the top most project name in the left hand file explorer (it should have a little blueprint icon). In the center column click on your project name under 'TARGETS', (it should have a little pencil 'A' icon next to it). Look under 'iPhone / iPod Deployment Info' and look for 'Main Interface'. You should be able to select an option from the drop down.
On top of "sho" answer, that is correct (fourth parameter of UIApplicationMain should be the name of the main controller), I add some comments.
I have recently changed the 'model' of an app of mine from using MainWindow.xib to construct a window programatically. The app used an older template that created that MainWindow automatically. Since I wanted to support a different controller view XIB for iPhone 5, it is easier to choose the right XIB programatically when the App Delegate is created. I removed MainWindow.xib from project as well.
Problem was, I forgot to fill the fourth parameter in UIApplication main and I FORGOT TO REMOVE MainWindow from "Main Interface" at Project Summary.
This caused a BIG problem: it rendered the harmless warning "Applications are expected to..." on development devices, but when it went to App Store, it broke on consumer phones, crashing because MainWindow was no longer in the bundle! I had to request an expedited review for the bugfix.
Another sympthom is that sometimes a white block, like a blank UIView, was sometimes appearing when Settings were changed and app was put in foreground. In iPhone 5 it was clear that it was an 320x480 block. Perhaps the missing MainWindow was being created in development mode, using the old size. I had just found this bug when the first reports of the crash reached the inbox.
Installing the app from App Store instead of from XCode showed that the app indeed crashed, and the MainWindow issue revealed itself on log, so I could see that it was not some special combination of devices+IOS versions.
To add to Mike Flynn's answer, since upgrading to Xcode 7 and running my app on an iOS 9 device, I added this to my (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Hide any window that isn't the main window
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow *window in windows) {
if (window != self.window) {
window.hidden = YES;
}
}
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.
This error also show up when file's owner of MainWindow.xib is set incorrectly.
File's owner is UIApplication
->inserted object of app delegate class with window outlet connected to window
精彩评论