入力欄付きUIAlertControllerサンプル
iOS8から登場したアラートコントローラー「UIAlertController」のメモ。
パスワード入力欄付き。
前提
selfはViewControllerね。
ソース
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"タイトル" message:@"メッセージ" preferredStyle:UIAlertControllerStyleAlert]; //入力欄(UITextField)付きを設定 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { //入力欄に薄くでるアレ textField.placeholder = @"パスワードを入力"; //マスク(*表示) textField.secureTextEntry = YES; }]; //キャンセルボタンと処理 UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"キャンセル" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:actionCancel]; //OKボタンと処理 UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //OKタップ時の処理 //alert.textFieldsにUITextFieldが入っている UITextField *textField = alert.textFields.firstObject; if ([textField.text isEqualToString:@"0000"]) { NSLog(@"パスワードOK"); } }]; [alert addAction:actionOK]; //表示 [self presentViewController:alert animated:YES completion:nil];
アクションボタンのスタイルはUIAlertActionStyleで指定。