【问题标题】:Attempt to present whose view is not in the window hierarchy alert尝试显示其视图不在窗口层次结构警报中
【发布时间】:2017-06-20 20:41:24
【问题描述】:

当我将以下代码放入 Viewdidload 时,我会返回一个警告:

警告:尝试呈现不在窗口层次结构中的视图!

我尝试将它放在 ViewdidAppear 中,但没有显示警报。

我该如何解决这个问题?

代码:

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Login"
                                                                              message: @"Input username and password"
                                                                       preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Public Key";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Private Key";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = YES;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * publickey = textfields[0];
        UITextField * privatekey = textfields[1];
        [[NSUserDefaults standardUserDefaults] setObject:publickey.text forKey:@"privateKey"];
        [[NSUserDefaults standardUserDefaults] setObject:privatekey forKey:@"publicKey"];
        NSLog(@"%@:%@",publickey.text,privatekey.text);
    }]];

谢谢。

【问题讨论】:

    标签: ios objective-c alert


    【解决方案1】:

    您不能在viewDidLoad 中显示任何内容是正确的,因为此时视图本身并未显示,因此本身无法显示另一个视图。

    但是,在 viewDidAppear 中显示它应该可以工作。但是您在此处发布的代码不包含 show 命令。你能检查一下你是否错过了移动这个吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多