【问题标题】:UIViewControllerHierarchyInconsistency Exception when displaying UIAlertController显示 UIAlertController 时的 UIViewControllerHierarchyInconsistency 异常
【发布时间】:2019-09-13 14:57:17
【问题描述】:

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

视图控制器是使用故事板创建的

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

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] *** 由于未捕获的异常“UIViewControllerHierarchyInconsistency”而终止应用程序,原因:“子视图控制器:<UICompatibilityInputViewController:0x13f5afd80> 应该有父视图控制器:<ViewController: 0x13f549360> 但请求的父级是:<UIInputWindowController: 0x140043c00>'

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

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

任何想法是什么导致了问题?

【问题讨论】:

标签: ios objective-c xcode


【解决方案1】:

我也遇到了这个异常,我的应用程序崩溃了。在我的例子中,我使用了 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
    }

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

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

【讨论】:

    【解决方案2】:

    我也遇到了这个问题,发现解决方法是你应该找到顶视图控制器并在顶视图控制器上显示警报。

    找到顶视图控制器:

    -(UIViewController *)getTopViewController{
                UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
            while (topController.presentedViewController) {
                topController = topController.presentedViewController;
            }
            if (![topController isKindOfClass:[NSNull class]]) {
                return topController;
        }
    

    并在顶视图控制器上显示警报:

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

    【讨论】:

    • 感谢您的回复。不幸的是,它并没有为我解决。同样的错误:(
    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 2019-02-27
    相关资源
    最近更新 更多