// 參數preferredStyle:是IAlertController的樣式 // UIAlertControllerStyleAlert 建立出來至關於UIAlertView // UIAlertControllerStyleActionSheet 建立出來至關於 UIActionSheet UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"大標題(正文)" message:@"小標題(副文)" preferredStyle:UIAlertControllerStyleAlert]; // 建立按鈕 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"肯定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { NSLog(@"注意學習"); }]; // 建立按鈕 // 注意取消按鈕只能添加一個 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) { // 點擊按鈕後的方法直接在這裏面寫 NSLog(@"注意學習"); }]; // 建立警告按鈕 UIAlertAction *structlAction = [UIAlertAction actionWithTitle:@"警告" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction *action) { NSLog(@"注意學習"); }]; // 添加按鈕 將按鈕添加到UIAlertController對象上 [alert addAction:okAction]; [alert addAction:cancelAction]; [alert addAction:structlAction]; // 只有在UIAlertControllerStyleAlert樣式狀況下才能夠添加文本框,UIAlertControllerStyleActionSheet樣式不支持文本框,會崩潰。 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"用戶名"; textField.secureTextEntry = YES; }]; // // 取出文本 // UITextField *text = alertController.textFields.firstObject; // UIAlertAction *action = alertController.actions.firstObject; // 將UIAlertController模態出來 至關於UIAlertView show 的方法 [self presentViewController:alert animated:YES completion:nil];