Home > DeveloperSection > Forums > How to identify UIActionSheet button in Objective-C?
Pooja Malohtra
Pooja Malohtra

Total Post:47

Points:331
Posted on    February-09-2016 7:03 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 445  View(s)
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!



Tarun Kumar

Total Post:213

Points:1635
Supported
Posted on    February-09-2016 7:26 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick