How to create a UIActionSheet and respond to it

This will bring up the UIActionSheet on iPhone devices from the bottom of the screen:

    // trigger a UIActionSheet
    UIActionSheet *myActionSheet;
    myActionSheet = [[UIActionSheet alloc]initWithTitle:@"My Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Cancel in Red" otherButtonTitles:@"Open in Safari", nil];
    myActionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
    [myActionSheet showInView:self.view];

You can add more buttons if you like by comma separating them. If you don’t feel the need for a button (for example the destructive button) simply set it to nil and it won’t be displayed.

In order to respond to it your class must conform to the UIActionSheetDelegate protocol. Here’s how you implement the method and query which button has been pressed:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    if (buttonIndex == 0) {
        NSLog(@"This was Button 0");
    if (buttonIndex == 1) {
        NSLog(@"This was Button 1");
    if (buttonIndex == 2) {
        NSLog(@"This was button 2");

The buttonIndex counts the buttons from top to bottom of how they are displayed, starting with 0.

One thought on “How to create a UIActionSheet and respond to it

Leave a Reply