尝试显示其视图不在窗口层次结构警报中

时间:2017-06-20 20:41:25

标签: ios objective-c alert

当我在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);
    }]];

感谢。

1 个答案:

答案 0 :(得分:2)

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

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