【4/20追記】 一つの画面で複数のUIAlertViewを制御するときに……
UIAlertViewを、状況ごとにいくつか出したいと思ったときに
参考書では特に触れられていないことが多いので、少し書いておきます。
簡単に書けば、こういうこと。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(alertView1 == alertView){ } else if (alertView2 == alertView){ ... } }
ヘッダファイルで、メンバ変数としてalertViewのオブジェクトを宣言しておいて
Delegateのイベント内で一致の判定を行うだけ。
※追記
メンバ変数として定義していなくても、以下のような方法を取れば
UIAlertViewを判別することが可能です。
この記事の応用になりますが、
http://d.hatena.ne.jp/corrupt/20110318/1300402045
static const NSInteger kTagAlert1 = 1; static const NSInteger kTagAlert2 = 2; ... 省略 ... UIAlertView* alert1 = [[[UIAlertView alloc] init] autorelease]; alert1.delegate = self; alert1.title = @"sample"; alert1.message = @"サンプルです。"; alert1.tag = kTagAlert1; [alert1 addButtonWithTitle:@"いいえ"]; [alert1 addButtonWithTitle:@"はい"]; alert1.cancelButtonIndex = 0; [alert1 show] ... 省略 ... - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(kTagAlert1 == alertView.tag){ ... } }
このような形で、tagを使えばメンバ変数として定義していなくても
判別を行うことが可能となります。
思えば、UIViewのサブクラスなんだから当たり前ですね。