iOS 其视图不在 window 层次结构中

iOS whose view is not in the window hierarchy

当我从 PassCode 控制器移动到 OTP ViewController 时,我在控制台中收到以下错误:

Warning: Attempt to present < OTPController: 0x1e56e0a0 > on < PassCodeController: 0x1ec3e000> whose view is not in the window hierarchy!

这是我用来在视图之间切换的代码:

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OTPViewController *lOTPViewController = [storyboard instantiateViewControllerWithIdentifier:@"OTPViewController"];
    lOTPViewController.comingFromReg = true;

    [self presentViewController:lOTPViewController animated:YES
                     completion:nil];

我正在展示来自注册的密码控制器ViewController:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        PassCodeViewController *passVC =  [storyboard instantiateViewControllerWithIdentifier:@"PassCodeViewController"];
        [self presentViewController:passVC animated:YES completion:nil];

发生这种情况是因为两个 viewcontroller 同时存在和关闭,或者您试图在 viewcontroller 打开 ViewDidload 方法时立即呈现 ViewController 所以

第一个:

  • viewDidAppear 方法中呈现 ViewController 或代替 ViewDidload

第二:

我建议使用完成方法来显示和关闭 viewcontrolr,如下例所示:

[self presentViewController:lOTPViewController animated:YES
                             completion:^{

        }];

更新:

创建一个单独的方法来呈现 OTPViewController,如下所示:

-(void)PresentOTPViewController
{

    UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OTPViewController *lOTPViewController = [storyboard instantiateViewControllerWithIdentifier:@"OTPViewController"];
    lOTPViewController.comingFromReg = true;

    [self presentViewController:lOTPViewController animated:YES
                     completion:^{}];

}

现在使用 performSelector

以 1 秒 Delaya 调用此方法
[self performSelector:@selector(PresentOTPViewController) withObject:self afterDelay:1.0 ];

你需要把上面的performselect代码放在

[self dismissViewControllerAnimated:YES completion:^{
 [self performSelector:@selector(PresentOTPViewController) withObject:self afterDelay:1.0 ];
}]; // this is the dismiss method of PassCodeViewController

t

尝试从rootViewController中呈现,

[self.view.window.rootViewController presentViewController:lOTPViewController animated:YES completion:nil];

使用下面的代码行..

// you need to create UIStoryboard object by giving name of your storyboard
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// here you need to create storyboard ID of perticular view where you need to navigate your app 
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewContIdentifire"];

// if use presentViewController this will not enables you to go back to previous view
[self presentViewController:vc animated:NO completion:nil];
                        **// OR**
// using pushViewController lets you to go back to the previous view
[self.navigationController pushViewController:vc animated:YES];