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

use*_*239 5 iphone xcode objective-c ios ios7

我正在尝试链接我的问题,UIViewController但我得到了最后的错误.

Attempt to present ViewController whose view is not in the window hierarchy
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

- (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];

    }
}
Run Code Online (Sandbox Code Playgroud)

代码错误:

Warning: Attempt to present <ViewController: 0x110634bc0> on <Login: 0x10951e7f0> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)

Bil*_*een 2

看来您的UIViewController( Login) 不在窗口层次结构中。

您可以将您的添加LoginViewController为 UIWindow 中的子视图。如果是这样,请将其设置为UIWindowrootViewController

AppDelegate.m

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

    //Other code parts

    [self.window setRootViewController:loginViewController];
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

或者

如果您将 LoginViewController 的视图添加为任何 UIViewController(例如FirstViewController)中的子视图,请改为呈现它

在你的FirstViewController.m中,

-(void)viewDidLoad{
    [super viewDidLoad];


    LoginViewController *loginViewController ;//Instantiate LoginViewController here
    [self presentViewController:loginViewController animated:NO completion:Nil];
}
Run Code Online (Sandbox Code Playgroud)