iOS whose view is not in the window hierarchy

2019-02-27 13:28发布

问题:

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

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

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

回答1:

That happen because of two viewcontroller present and dismiss at a same time or you are trying to present ViewController immediately at the viewcontroller open ViewDidload method so

First:

  • Present ViewController from viewDidAppear Method or instead of ViewDidload.

Second:

I suggest to make use of completion method for present and dismiss viewcontrolelr like following example:

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

        }];

UPDATE:

Create a separate method of presenting a OTPViewController like following:

-(void)PresentOTPViewController
{

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

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

}

Now call this method with 1 second Delaya using performSelector

[self performSelector:@selector(PresentOTPViewController) withObject:self afterDelay:1.0 ];

You need to put above performselect code in

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

t



回答2:

Try to present it from rootViewController,

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



回答3:

Use below line of code ..

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