LoginSignup
9
8

More than 5 years have passed since last update.

[Objective-C] UIViewAlertメモ

Last updated at Posted at 2014-03-07

JSならalert('OK');だけで出てくれるのに、色々と初期化が大変です。(その分、機能豊富ですが)
IDEに補完任せてると一向に覚えないのでメモとして残しておきます。

UIAlertViewを生成

色々まとめて初期化

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"タイトル"
                                                message:@"メッセージ"
                                               delegate:self
                                      cancelButtonTitle:@"キャンセル"
                                      otherButtonTitles:@"ボタン1", @"ボタン2", @"ボタン3", nil];

最後のotherButtonTitles:の引数は可変引数です。なので最後はnil

個別に設定

UIAlertView *alert = [[UIAlertView alloc] init];
alert.title    = @"タイトル";
alert.message  = @"メッセージ";
alert.delegate = self;
alert.cancelButtonIndex = 0;
[alert addButtnWithTitle:@"キャンセル"];
[alert addButtonWithTitle:@"OK"];
[alert show];

デリゲートで押したボタンを判別

- (void)alertView:(UIAlertview *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0: {
            // 0番目のボタン
            break;
        }
        case 1: {
            // 1番目のボタン
            break;
        }
        // …以下必要な分続く
    }
}

UIAlertViewを同期的に使う

色々問題はありますが、一応同期的に扱う方法があります。
それについては個別に書いているので、[Objective-C] UIAlertViewを同期処理するを見てください。

Associated Objectについての解説はこちらに移動しました。

9
8
1

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
9
8