Detecting button pressed when there are multiple alert views

12,896

Solution 1

It would be more technical as well better that set unique tag for separate UIAlertView and identify it and access in its delegate method.

For example,

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Are You Sure you want to Update?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
    [alert setTag:1];
    [alert show];
    [alert release];

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
    {
        if(alertView.tag == 1)
        {
            // set your logic
        }
    }

Solution 2

Use tag property to uniquely identify each of the alertview u create.

Like this

myAlertView.tag = 1

Then in the clickedButtonAtIndex delegate method check which alertview's button was clicked using this tag property ,

if(alertView.tag==1)

Solution 3

I wouldn't use the titles to distinguish between the buttons. You'll run into problems when your app is localized or you decide to change the button titles, but forget to update them everywhere. Use the button indexes instead or if you only have one button in addition to a cancel button, use the cancelButtonIndex property of UIAlertView.

To distinguish between multiple alert views, you could use their tag property.

Solution 4

In your view, add a property for each alert view.

UIAlertView *myAlertType1;
UIAlertView *myAlertType2;

@property (nonatomic, retain) UIAlertView *myAlertType1;
@property (nonatomic, retain) UIAlertView *myAlertType2;

Create your alert using these properties

self.myAlertType1 = [[[UIAlertView alloc] initWithTitle: ... etc] autorelease];
[self.myAlertType1 show];

Then in your delegate method:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView == myAlertType1) {
         // check the button types and add behaviour for this type of alert
    } else if (alertView == myAlertType2 {
         // check the button types and add behaviour for the second type of alert
    }
}

Edit: Although the above works, iApple's suggestion of using the tag seems cleaner/simpler.

Solution 5

 //in your .h file
  UIAlertView* alert1;
  UIAlertView* alert2;

  //in your .m file
  // when you are showing your alerts, use
  [alert1 show]; //or
  [alert2 show];

  //and just check your alertview in the below method

  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  {  
       if(alertView == alert1)
       {
              //check its buttons
       }
       else   //check other alert's btns
  }   
Share:
12,896

Related videos on Youtube

Jack Humphries
Author by

Jack Humphries

Updated on June 14, 2022

Comments

  • Jack Humphries
    Jack Humphries almost 2 years

    I have multiple alert views in one view, and I use this code to detect which button was pressed:

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
    
        NSString *title = [alertView buttonTitleAtIndex:buttonIndex];  
    
        if ([title isEqualToString:@"OK"]) {
    
              //for one alert view
              [passCode becomeFirstResponder];
    
         } else if ([title isEqualToString:@" OK "]) {
    
            //for another alert view, had to change "OK" to " OK "
            [passCodeConfirm becomeFirstResponder];
    
        }
    }   
    

    Now since there are multiple alert views in one view that do different things, I have to trick the user into thinking "OK" and " OK " are the same thing. It works and looks fine, but it feels kind of messy. Surely there is another way to do this, such as making this specific to an alert view, and then making it specific to another. Do you know how I would do this? Thanks!

  • gamozzii
    gamozzii over 12 years
    I like this better than my solution above
  • Jack Humphries
    Jack Humphries over 12 years
    Great, thanks for your help! Can tags be assigned to pretty much any UI object?
  • alloc_iNit
    alloc_iNit over 12 years
    All of the UI objects which are supporting getter/setter property for tag, developer can assign. Provided, for same kind of class must be having a unique tag to get response properly.
  • Joshua Dance
    Joshua Dance over 10 years
    I don't like using tags because you have to add and access extra information when what the object is should tell you what you need to know.
  • Pavan
    Pavan about 10 years
    I advice against having magic numbers in your code, it really does make your code difficult to read, and I find myself having to search magic numbers within my own code just to find out which object relates to which number. Follow Gamozzii's method in his post below.
  • alloc_iNit
    alloc_iNit about 10 years
    @Pavan - I think it's not a good practice to create multiple UIAlertView objects. I prefer to go with a unique tag. Of course, one can define meaningful macros to use across a project.
  • Pavan
    Pavan about 10 years
    @alloc_iNit I've given logical reasons for the preferred method. Could you do the same for your method so that you can justify your argument pls. Cheers buddy.