Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: two UIAlert with two different delegate methods

I have an UIAlert

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ok" 
                                                        message:@"Canc?"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:@"Annul", nil];
[alertView show];
[alertView release];

and its delegate method:

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

if(buttonIndex == 0)//OK button pressed
{
    //do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
    //do something
}

and it's all ok but if I have another alertview example "alertViewOne", I want that this alertViewOne have its delegate method and it shouldn't use delegate method of first alertview; how does change my code?

like image 960
cyclingIsBetter Avatar asked Jun 08 '11 09:06

cyclingIsBetter


1 Answers

Simply set a tag to each Alert view and check which one sent the messeg.

alertView.tag=0;

And then

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex              {
  if(alertView.tag==0){

     if(buttonIndex == 0)//OK button pressed
    {
    //do something
    }
    else if(buttonIndex == 1)//Annul button pressed.
    {
    //do something
    }
}else{
    if(buttonIndex == 0)//OK button pressed
    {
    //do something
    }
      else if(buttonIndex == 1)//Annul button pressed.
    {
    //do something
    }
  }

Update There is a better solution using blocks.

You can look at this project for example: UIAlertView-Blocks

And as far as I know iOS8 will come with native alerts with blocks.

like image 90
shannoga Avatar answered Oct 20 '22 12:10

shannoga