How to identify UIActionSheet button in Objective-C?

Total Post:47

Points:331
 1125  View(s)
Ratings:
Rate this:

I have created an UIActionSheet using below code in our iPhone application:

UIActionSheet *actionSheet = [[UIActionSheet alloc]
                               initWithTitle:@"ActionSheetSample"
                               delegate:self
                               cancelButtonTitle:@"Cancel"
                               destructiveButtonTitle:nil
                               otherButtonTitles:@"Button1",@"Button2",@"Button3", nil];
[actionSheet showInView:self.view];

Now, my action sheet created successfully but my problem is how can I identify which button is clicked.!
Please help me!

  1. Post:214

    Points:1642
    Re: How to identify UIActionSheet button in Objective-C?

    UIActionSheet provides a delegate method actionSheet:clickedButtonAtIndex: when the user clicks a button on an action sheet.
    For Example:

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
      if(buttonIndex == 0)
      {
         NSLog(@"First Button is Clicked");
      }
      else if(buttonIndex == 1)
      {
         NSLog(@"Second Button is Clicked");
      }
    }

    Above method have two parameters actionSheet and buttonIndex.
    actionsheet: contains the action button, and
    buttonIndex have the button indices which will start at 0.

Answer