UIAlertViewDelegate: clickedButtonAtIndex and two buttons

guys:

There are two buttons in my viewController of the test application, on the right I call it “NO”,

and the other is YES. Two buttons will call two different functions, and when

click one of the buttons, I want to show the user a warning to confirm this.

I know that UIAlertViewDelegate

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

but there are two buttons, I am puzzled. How to find out which button is pressed.

So help me with this, thanks in advance!

+6
source share
3 answers

When you create a UIAlertView , you can set a tag for it

 -(IBAction)yesButtonClick:(id)sender{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil]; alert.tag = 101; [alert show]; } -(IBAction)noButtonClick:(id)sender{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil]; alert.tag = 102; [alert show]; } 

In the delegate method, check which warning is displayed

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 101) { // from YES button } else if (alertView.tag == 102) { // from NO button } } 
+17
source
 - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ switch(buttonIndex){ case 0: //YES button handler break; case 1: //NO button handler break; default: break; } } 
0
source

you can use the tag attribute to make the difference between your tug UIAlertView
in button function 1
alertView1.tag=1;
and in

 -(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if(actionSheet.tag==1){ //first button was clicked } } 
0
source

Source: https://habr.com/ru/post/912231/


All Articles