0
2

More than 3 years have passed since last update.

[objective-c]UIArratControllerでのパスワード入力ダイアログについて

Posted at

ソース

//パスワードアラート表示

//ここにパスワード格納、実際にはDBとかから取得
NSString    *password = @"testpass";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
message:@"パスワードを入力してください" preferredStyle:UIAlertControllerStyleAlert];

//入力欄(UITextField)付きを設定
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//入力欄の背景に薄く出る
textField.placeholder = @"パスワードを入力";
//マスク(・表示)
textField.secureTextEntry = YES;

// addActionした順に左から右にボタンが配置されます
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // OKボタンが押された時の処理
    UITextField *textField = alertController.textFields.firstObject;
    if ([textField.text isEqualToString:password]) {
        //パスワード合ってた時の処理
    }else{
        //パスワード違ったら警告だす
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告"message:@"パスワードが一致しません" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            return;
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}]];
[self presentViewController:alertController animated:YES completion:nil];

この処理はViewControllerに記載する必要があります。
そうでないと↓で(たしか)エラーが出ます。
[self presentViewController:alertController animated:YES completion:nil];

脱線

Objective-cで作られてるソースってそもそも古いものが多いと思います。
そのためダイアログ表示をUIAlertViewで作ってるものがそこそこあり、
もし今後UIAlertViewが非推奨ではなくAPPstoreに挙げられなくなったらUIAlertControllerに書き換える必要がありますが
ものすごく大変そうです・・・

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2