“Application windows are expected to have a root view controller” conditional appearance

旧时模样 提交于 2019-12-31 04:09:28

问题


I'm writing an app for iPhone using Xcode 4.5 and iOS6. I'm also creating a new UIWindow to be able to manage the area of the status bar (to display messages there, etc.) I'm using storyboards and my appDelegate method looks like this:

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

The message does not appear in the console when I put it in the method called viewDidAppear:

- (void)viewDidAppear:(BOOL)animated     {

    if (!window) {
        window = [[SGStatusBar alloc] initWithFrame:CGRectZero];
        window.frame = [[UIApplication sharedApplication] statusBarFrame];
        window.alpha = 0.5f;

        [self.view.window makeKeyAndVisible]; // has to be main window of app
        window.hidden = NO;
    }  
}

The same method, put in the viewDidLoad gives a warning in the console:

2012-12-27 11:34:20.838 NewApp[433:c07] Application windows are expected to have a root view controller at the end of application launch

Is this because I've created a new UIWindow? Why the difference between those two methods is so big?

And, most importantly, how can I get rid of this warning while putting the code in the viewDidLoad method?

EDIT:

I have encountered the same problem here, but it's not the way I'd like to solve it (it's actually the way I'm solving it right now)

I've tried setting my current ViewController as my window's root view controller by doing this:

ViewController *vcB = [[UIViewController alloc] init];
window.rootViewController = vcB;

But I keep getting a warning that says:

Incompatible pointer types initializing 'ViewController *__strong' with an expression of type 'UIViewController *'

回答1:


Set window.rootViewController property .




回答2:


Add the following code to your delegate.h and delegate.m files.

AppDelegate.h

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) YourViewController *viewController;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[YourViewcontroller alloc] initWithNibName:@"YourViewcontroller" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

Hope it works.



来源:https://stackoverflow.com/questions/14051129/application-windows-are-expected-to-have-a-root-view-controller-conditional-ap

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