gpt4 book ai didi

ios - 显示 UIAlertController 时出现 UIViewControllerHierarchyInconsistency 异常

转载 作者:行者123 更新时间:2023-11-29 11:55:51 29 4
gpt4 key购买 nike

当我尝试显示 UIAlertController在我的应用程序中,应用程序终止并出现异常 UIViewControllerHierarchyInconsistency .

View Controller 是用 Storyboard创建的

From Storyboard editor

我创建并(尝试)显示这样的警报:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *yesButton = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
...
}];
[alert addAction:yesButton];

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

但是,在执行过程中,我在输出中得到了这个:

2016-08-25 09:46:07.536 TrackYou[10554:3165715] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x13f5afd80> should have parent view controller:<ViewController: 0x13f549360> but requested parent is:<UIInputWindowController: 0x140043c00>'

来自同一个ViewController我用 presentViewController展示一个自定义的 ViewController,它工作正常。

SettingsViewController *controller = [SettingsViewController new];
[self presentViewController:controller animated:YES completion:nil];

知道是什么导致了这个问题吗?

最佳答案

我也遇到了这个异常,我的应用程序崩溃了。在我的例子中,我使用了 UIAlertController 并自定义了 UI,这样 UIAlertController 就会有一个标签和一个并排的文本字段。

对于 UIAlertController 的自定义 UI,我使用了以下代码。

    let alert = UIAlertController(title: "your title",
message: "your msg",
preferredStyle: .alert)


let inputFrame = CGRect(x: 0, y: 90, width: 300, height: 60)
let inputView: UIView = UIView(frame: inputFrame);

let prefixFrame = CGRect(x: 10, y: 5, width: 60, height: 30)
let prefixLabel: UILabel = UILabel(frame: prefixFrame);
prefixLabel.text = "your label text";

let codeFrame = CGRect(x: 80, y: 5, width: 235, height: 30)
let countryCodeTextField: UITextField = UITextField(frame: codeFrame);
countryCodeTextField.placeholder = "placeholder text";
countryCodeTextField.keyboardType = UIKeyboardType.decimalPad;

let submitAction = UIAlertAction(title: "Submit", style: .default, handler: { (action) -> Void in

// your code after clicking submit
})

let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in })


alert.addTextField { (textField) in
textField.inputView = countryCodeTextField
}


inputView.addSubview(prefixLabel);
inputView.addSubview(countryCodeTextField);


alert.view.addSubview(inputView);

alert.addAction(submitAction)
alert.addAction(cancel)


present(alert, animated: false, completion: nil)

当我运行这段代码时,我得到了异常

  *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x13f5afd80> should have parent view controller:<ViewController: 0x13f549360> but requested parent is:<UIInputWindowController: 0x140043c00>'

然后我删除了下面的代码

  alert.addTextField { (textField) in
textField.inputView = countryCodeTextField
}

我的异常消失了,我的应用程序不再崩溃。

希望这对某人有帮助。抱歉,如果看不懂,我的英语不太好。

关于ios - 显示 UIAlertController 时出现 UIViewControllerHierarchyInconsistency 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39144022/

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