2011-09-13 6 views
2
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"download"]; 
    [alert show]; 
    [alert release]; 

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     if (buttonIndex == 0) 
     { 
      //Code for OK button 
     } 
     if (buttonIndex == 1) 
     { 
      //Code for download button 
     } 
    } 

罰金は,,私は2 uialertsを持っており、ケースと第一uialert含まれています(OK &ダウンロード)ボタンの両方に自己に設定デリゲート秒(&アップロードをキャンセル)ボタンが含まれて言います今私たちは別々のイベントハンドラを知る必要がありますか?洗面所独立イベントハンドラ

答えて

12

UIView内で複数のUIAlertViewを処理するには、それぞれに固有のタグを設定する必要があります。

alert.tag = 123; 

また、代理メソッドからの応答を得るには、それぞれ固有のタグで管理します。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     if(alertView.tag == 123) 
     { 
      if (buttonIndex == 0) 
      { 
       //Code for OK button 
      } 
      else if (buttonIndex == 1) 
      { 
       //Code for download button 
      } 
     } 
     else if(alertView.tag == 456) 
     { 
      // code to manage another alertview response. 
     } 
    } 
+1

ただし、タグ値には、enumまたは#defineを使用してください。私はあらゆる種類の '.tag = 1234'とviewWithTag:1234コードを見るのは嫌です。名前付き定数を使う! 'if(alertView.tag == kDownloadAlertView){...}'ははるかに読みやすいです! –

+0

マジックナンバーは悪い考えです。 '123'の代わりに、各警告ビューの値を持つ' enum'を宣言します。例えば。列挙型ClassNameAlertViews {ClassNameConfirmCancelDownloadAlert = 123、ClassNameConfirmAnotherUserActionThatYouMaybeAbleToRemoveByAddingUndoSupportAlert = 234}; –

+0

@Mike Weller&@ Benedict Cohen:そうだよ。ご意見をいただきありがとうございます。 –

4

例、コールバックで再びそれらのタグを戻って確認し、そこに残りの部分を行い、2つの異なるUIAlertViewインスタンスに対してtagプロパティを設定してみてくださいと:

UIAlertView *alertDownload = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"download"]; 
    alertDownload.tag = 1; 
    [alertDownload show]; 
    [alertDownload release]; 


UIAlertView *alertUpload = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"upload"]; 
    alertUpload.tag = 2; 
    [alertUpload show]; 
    [alertUpload release]; 

そしてここでは、委任コールバックです、

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(alertView.tag == 1) { 
      //Here you do your stuff for Download 
    } 
    if(alertView.tag == 2) { 
     //Here you do stuff for Upload 
    } 
} 
1
   my_Alert = [[UIAlertView alloc]initWithTitle:@"Hi" message:@"Hello" delegate:self 
       cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil]; 
      my_Alert.frame = CGRectMake(462, 359, 400, 50); 

      my_Alert.tag = 1; 

       my_Alert = [[UIAlertView alloc]initWithTitle:@"Hi" message:@"Hello" delegate:self 
       cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil]; 
      my_Alert.frame = CGRectMake(462, 359, 400, 50); 

           my_Alert.tag = 2; 

     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 

    if (buttonIndex==0 && my_Alert.tag == 1) 
    { 

     NSLog(@"Perform action on button touch of index 0 of First Alert"); 
    } 
     else 
     { 
      NSLog(@"Perform action on button touch of index 1 of First Alert"); 
     } 

    if (buttonIndex==0 && my_Alert.tag == 2) 
    { 
    NSLog(@"Perform action on button touch of index 0 of Second Alert");   
    } 
      else 
     { 
      NSLog(@"Perform action on button touch of index 1 of Second Alert"); 
     } 
    } 
0

多くのアラートビューがある場合、またはデマンドメソッドが複雑な場合、各アラートビューを管理するコントローラ(単純なNSObjectサブクラス)を作成することができます。 DownloadConfirmationAlertController。メインコントローラは、サブコントローラへの参照を保存できます。

関連する問題