UIViewControllerHierarchyInconsistency显示UIAlertController

时间:2016-08-25 11:27:04

标签: ios objective-c xcode

当我尝试在我的应用中显示UIAlertController时,应用终止时会遇到异常UIViewControllerHierarchyInconsistency

使用故事板

创建视图控制器

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

从同一个ViewController我使用presentViewController来呈现自定义ViewController,它运行正常。

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

任何想法导致问题的原因是什么?

2 个答案:

答案 0 :(得分: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
    }

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

希望这对某人有帮助。抱歉,如果无法理解,我的英语不太好。

答案 1 :(得分:0)

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

查找顶部视图控制器:

function populateForm(id) {
$("form[name='application']").$("form[element='partyid']").val = id ;
} 

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

-(UIViewController *)getTopViewController{
            UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

        while (topController.presentedViewController) {
            topController = topController.presentedViewController;
        }
        if (![topController isKindOfClass:[NSNull class]]) {
            return topController;
    }