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

5
我在试图链接我的 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!

你能复制粘贴详细的错误吗? - highwing
如果根视图控制器的视图当前不在窗口层次结构中,则无法执行此操作。 - rdelmar
@highwing,刚刚添加了。 - user3546239
@rdelmar,唉,当提醒弹出时,我唯一需要做的就是将它们重定向回去。 - user3546239
@user3546239,为什么你要从rootViewController呈现vc?难道不应该从当前的视图控制器呈现吗? - highwing
@highwing,当我将另一个控制器添加为初始控制器时,它会失败并显示该消息,但是当我更改我的初始控制器时,它可以正常工作。 - user3546239
1个回答

2
似乎你的UIViewController(Login)不在窗口层级中。
如果是这样,你可能会将LoginViewController作为子视图添加到UIWindow中。 如果是这样,请将其设置为UIWindowrootViewController AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //Other code parts

    [self.window setRootViewController:loginViewController];
    return YES;
}

或者

如果您正在将LoginViewController的视图添加为UIViewController的子视图(例如FirstViewController),请改用present方式呈现。

在您的FirstViewController.m文件中,

-(void)viewDidLoad{
    [super viewDidLoad];


    LoginViewController *loginViewController ;//Instantiate LoginViewController here
    [self presentViewController:loginViewController animated:NO completion:Nil];
}

没有运气。我同时使用UIViewController。当我设置其他UIViewController时,重定向有效,但是当我放置另一个UIViewController(登录)时,它会卡住。 - user3546239
@user3546239,问题是:其他UIViewController和LOGIN之间有什么不同?你是否将正确的故事板ID分配给了LOGIN? - highwing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接