IOS: two UIAlert with two different delegation methods

I have a UIAlert

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

and its delegation 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 everything is fine, but if I have another example of alertview "alertViewOne", I want this alertViewOne to have its own delegation method and should not use the delegation method of the first warning; how does my code change?

+8
ios objective-c xcode uialertview
source share
1 answer

Just set a tag for each type of alert and check which one is sent by 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 have built-in warnings with blocks.

+20
source share

All Articles