视图控制器未在App Delegate中显示

时间:2013-08-23 06:27:07

标签: iphone ios objective-c uiviewcontroller appdelegate

我正在尝试加载一个viewController,如果没有帐户登录但是它给了我这个错误Warning: Attempt to present <DefaultViewController: 0xb96cac0> on <MainViewController: 0xb95c320> whose view is not in the window hierarchy!  这是我现在使用的代码:

BOOL returnAllOff = [[User data] returnAllOff];

if (returnAllOff) {
    DefaultViewController *viewController =[[DefaultViewController alloc]
    init];
    UIViewController *presentingController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    [presentingController presentViewController:viewController animated:YES completion:nil];

}

2 个答案:

答案 0 :(得分:1)

实际上这是从AppDelegate加载ViewController的正确方法..

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

    DefaultViewController *viewController = [[DefaultViewController alloc] initWithNibName:@"DefaultViewController" bundle:nil];
    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

答案 1 :(得分:1)

通过在appDElegate中导入DefaultViewController来试试这个。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
DefaultViewController *viewController = [[DefaultViewController alloc] init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];