こんばんはー
今日の内容はUIAlertViewです。
一つのクラスで何回か使い、各々処理を変えたいときありますよね。
そんなとき使う方法です。
まず、以下がアラートビューのデリゲートメソッドです
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
引数が2つあります。(UIAlertView *)alertViewと(NSInteger)buttonIndexです。
alertViewはUIViewなので、tag付けられるんですね。
てことで、
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"yyyyyy"
message:@"xxxxxx"
delegate:self
cancelButtonTitle:@"いいえ"
otherButtonTitles:@"はい", nil];
alert.tag=100;
[alert show];
をアラート呼びたいタイミングで書いて、デリゲートメソッドを以下のようにしてあげればOKです。
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100)
{
switch (buttonIndex)
{
case 0:
NSLog(@"「いいえ」が押されました。");
break;
case 1:
NSLog(@"「はい」が押されました。");
break;
default:
break;
}
}