【发布时间】: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