【问题标题】:iOS whose view is not in the window hierarchy视图不在窗口层次结构中的 iOS
【发布时间】:2016-09-03 18:31:18
【问题描述】:

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

警告:尝试在 on 其视图不在窗口层次结构中!

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

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

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

我正在从 RegistrationViewController 展示密码控制器:

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

【问题讨论】:

  • 从您呈现的位置 PassCodeController
  • UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];改用 self.storyboard
  • AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; appDelegate.window.rootViewcontroller 存在...
  • @PKT 这与情节提要无关,请大家避免
  • 谢谢大家。这段代码对我有用: OTPViewController *lOTPViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OTPViewController"]; lOTPViewController.comingFromReg = true; [self.navigationController pushViewController:lOTPViewController 动画:YES];

标签: ios objective-c uiview uinavigationcontroller presentviewcontroller


【解决方案1】:

这是因为两个视图控制器同时存在和关闭,或者您试图在视图控制器打开 ViewDidload 方法时立即显示视图控制器

第一:

  • 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 秒的延迟调用此方法

[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

【讨论】:

  • 是的,问题是 otp vc 已被解雇,您立即呈现新 vc,因此在解雇和新 vc 呈现之间添加 1-2 秒延迟
  • 我现在正在吃午饭,我会在 15 分钟内更新答案??
【解决方案2】:

尝试从rootViewController呈现,

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

【讨论】:

  • @Vijay .....检查更新的问题...stackoverflow.com/q/37110406/6285383
  • 代替 self 关键字来呈现您的视图控制器尝试使用 self.view.window.rootViewController。在您遇到该错误的地方使用此代码!
【解决方案3】:

使用下面的代码行..

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多