iOS视图不在窗口层次结构中的

时间:2016-05-09 07:55:28

标签: ios objective-c uiview uinavigationcontroller presentviewcontroller

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

  

警告:尝试出示< OTPController:0x1e56e0a0> on<   PassCodeController:0x1ec3e000>其视图不在窗口层次结构中!

这是我用来更改观看次数的代码:

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

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

我将从RegistrationViewController呈现PassCode控制器:

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

3 个答案:

答案 0 :(得分:3)

发生这种情况是因为两个viewcontroller同时存在和解除,或者你试图在viewcontroller open ViewDidload方法中立即呈现ViewController所以

<强>首先

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

<强>第二

我建议使用目前的完成方法并解除viewcontrolelr,如下例所示:

[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

答案 1 :(得分:2)

尝试从rootViewController中显示它,

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

答案 2 :(得分:1)

使用以下代码行..

// 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];
相关问题