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
        }
    }

fine,,say i have 2 uialerts and delegate set to self in both the cases and first uialert contains (ok & download)buttons second contains (cancel & upload)buttons now we need separate event handlers know?

4 Answers 4

12

To handle multiple UIAlertView within a UIView, you have to set unique tag for each.

    alert.tag = 123;

And while get response from delegate method manage each with unique tag.

    - (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.
       }
    }
4
  • 1
    But please, use an enum or #define for your tag values. I hate seeing all sorts of .tag = 1234 and viewWithTag:1234 code. Use named constants! if (alertView.tag == kDownloadAlertView) { ... } is far more readable! Sep 13, 2011 at 7:03
  • Magic numbers are a bad idea. Instead of 123, declare an enum with a value for each alert view. E.g. enum ClassNameAlertViews {ClassNameConfirmCancelDownloadAlert = 123, ClassNameConfirmAnotherUserActionThatYouMaybeAbleToRemoveByAddingUndoSupportAlert = 234}; Sep 13, 2011 at 7:08
  • @Mike Weller & @ Benedict Cohen: Sure sir. Thanks for your feedback.
    – alloc_iNit
    Sep 13, 2011 at 7:09
  • Just explicitly set the first enum value to 1, then let the rest be assigned automatically. I don't know where this '1234' thing came from, but everyone seems to do it. Sep 13, 2011 at 7:21
4

Try setting tag property for two different UIAlertView instances and then check back those tags again in the callback and do the rest there, example:

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];

And here is the Delegate CallBack,

-(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

If you have a lot of alert views or the delegate methods are complex then you could create a controllers (an simple NSObject subclass) to manage each alert view, e.g. DownloadConfirmationAlertController. Your main controller can then store a reference to the sub-controllers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.