0
votes

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.

1

1 Answers

2
votes

That you cannot show anything in the viewDidLoad is correct, because the view itself is not shown at this point, so there is no way itself can show another view.

However, showing it in viewDidAppear should work. But the code you posted here does not include a show command. Can you check if you missed to move this?