IOS: two UIAlert with two different delegate methods

蹲街弑〆低调 提交于 2019-12-08 17:07:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6276819/ios-two-uialert-with-two-different-delegate-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!