gpt4 book ai didi

View 不在窗口层次结构中的iOS

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:12:30 25 4
gpt4 key购买 nike

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

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

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

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

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

我正在展示来自 RegistrationViewController 的 PassCode Controller :

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

最佳答案

发生这种情况是因为两个 View Controller 同时存在和关闭,或者您试图在 View Controller 打开 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

关于 View 不在窗口层次结构中的iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37110406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com