代码不在窗口层次结构根视图中

时间:2014-05-20 04:11:15

标签: ios iphone objective-c xcode ios7

我在尝试关联UIViewController时遇到问题,但我收到了最后的错误。

Attempt to present ViewController whose view is not in the window hierarchy

这是我的代码:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Wokay"])
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Vibes"];
        [self.window.rootViewController presentViewController:vc animated:YES completion:nil];

    }
}

代码错误:

Warning: Attempt to present <ViewController: 0x110634bc0> on <Login: 0x10951e7f0> whose view is not in the window hierarchy!

1 个答案:

答案 0 :(得分:2)

您的UIViewControllerLogin)似乎不在窗口层次结构中。

您可能会在UIWindow中添加LoginViewController作为子视图。 如果是,请将其设置为UIWindow的{​​{1}}

<强> AppDelegate.m

rootViewController

如果要在任何UIViewController(例如- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //Other code parts [self.window setRootViewController:loginViewController]; return YES; } )中将LoginViewController的视图添加为subView,请改为显示

FirstViewController.m 中,

FirstViewController