2017-06-20 63 views
0

當我把下面的代碼中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); 
    }]]; 

感謝。

回答

2

您無法在viewDidLoad中顯示任何內容是正確的,因爲視圖本身在此處未顯示,因此本身無法顯示其他視圖。

但是,顯示它在viewDidAppear應該工作。但是您在此處發佈的代碼不包含show命令。你可以檢查你是否錯過了這個動作?

+0

它確實現在在viewDidAppear中工作。 – Anoniem

相關問題