首页 文章

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

提问于
浏览
2

While i am Moving From PassCode Controller to OTP ViewController, iam getting the following error in console:

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

This is the code I'm using to change between views:

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

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

i am presenting PassCode Controller From RegistrationViewController:

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

3 回答

  • 3

    这是因为两个viewcontroller同时存在和解除,或者你试图在viewcontroller打开 ViewDidload 方法时立即呈现ViewController所以

    First:

    • 显示来自 viewDidAppear 方法的ViewController或代替 ViewDidload .

    Second:

    我建议使用完成方法当前和解除viewcontrolelr如下例:

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

    UPDATE:

    创建一个单独的方法来呈现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 ];
    

    您需要在上面执行selectselect代码

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

    Ť

  • 2

    尝试从rootViewController呈现它,

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

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

相关问题