ROADTO みちログ

ひとのみちのブログ。大阪でiOSアプリの道を歩くフリーランス。

入力欄付き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];

f:id:hitonomichi:20150203000206p:plain

アクションボタンのスタイルはUIAlertActionStyleで指定。

自己紹介
メガネは売っていません
高浜 一道(たかはま かずみち)
大阪でiOSアプリの道を歩くフリーランス。
iPad・iPhoneとサーバを連携した、JSONやDBといったキーワードが出てくるツール系が多めです。
お仕事のご依頼やご連絡はこちらから。
お仕事ください!
Web : ROADTO
ML : k-takahama(a)roadto.jp

個人的なアカウントはこちら。
Tw : @hitonomichi
Fb : kazumichi.takahama