When I put the following code in Viewdidload I returns a warning:
Warning: Attempt to present on whose view is not in the window hierarchy!
I tried putting it in ViewdidAppear, but then the alert isn't showing up.
How can I fix this?
Code:
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);
}]];
Thanks.