Simple Menus and Messages with UIActionSheet

If you need to create a quick menu or present a user with a short message, you may want to consider a UIActionSheet as an option. This control will slide up from the bottom of the screen, and offers a number of easily configurable options.

The code in the interface definition below creates an actionsheet (if you prefer, you can accomplish the same using Interface Builder to layout and define a UIActionSheet). Notice there is a delegate required if you define multiple buttons on the sheet and need to determine which was tapped:

@interface SandboxViewController : UIViewController <UIActionSheetDelegate>
{
  ...
  UIActionSheet *sheet;
}
@end

At some point in your code you can create a sheet as follows:

  sheet = [[UIActionSheet alloc] initWithTitle:@"Select Belgian Beer Style"
                                      delegate:self
                             cancelButtonTitle:@"Cancel"
                       destructiveButtonTitle:nil
                            otherButtonTitles:@"Dubble", @"Lambic", @"Quadrupel", @"Strong Dark Ale", @"Tripel", nil];
 
  // Show the sheet
  [sheet showInView:self.view];
  [sheet release];

Here is another variation:

  sheet = [[UIActionSheet alloc] initWithTitle:@"Select Account to Delete"
                                      delegate:self
                             cancelButtonTitle:@"Cancel"
                        destructiveButtonTitle:@"Delete All Accounts"
                             otherButtonTitles:@"Checking", @"Savings", @"Money Market", nil];
  // Show the sheet
  [sheet showInView:self.view];
  [sheet release];

The screenshots below show each of the above sheets and how they look on the simulator:

To determine which button was selected, implement the method shown below:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
  NSLog(@"Button %d", buttonIndex);
}

One additional way to use an actionsheet is to present information to a user, in this case, you fill in the title and just one button:

  sheet = [[UIActionSheet alloc] initWithTitle:@"Lorem ipsum dolor...."
                                      delegate:self
                             cancelButtonTitle:nil
                        destructiveButtonTitle:nil
                             otherButtonTitles:@"Got it." ,nil];
 
  // Show the sheet
  [sheet showInView:self.view];
  [sheet release];

  1. Thanks for these articles! As a newbie, I find them extremely helpful as well as well written and attention-keeping!

  2. Thanks a lot friend. Every such article saves both time and nerves of developers.

  3. Is it fair to assume you could embed any control in a UIActionSheet, i.e. a UIPIckerView?

    • I’m not sure if it’s quite that simple given the various layout requirements, etc. If you explore this further, please post another comment with what you find.

  4. This is great. Works as advertised. There are lots of apps that use this, but I couldn’t figure out how they did it until I found this page. Thanks.

    I use the following code to add the UIBarButtonSystemItemAction button

    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
    target:self
    action:@selector(loadActionSheet:)];

  5. This is great.Now how to dismiss action sheet automatically. with out tap any button for particular time.

    for that i used

    for (UIWindow* window in [UIApplication sharedApplication].windows) {
    for (UIView* view in window. subviews) {
    BOOL alert = [view isKindOfClass:[UIAlertView class]];
    BOOL action = [view isKindOfClass:[UIActionSheet class]];
    if (alert || action){
    //UIAlertView *alert = (UIAlertView*)view;
    //[alert dismissWithClickedButtonIndex:-1 animated:NO];

    UIActionSheet *alert123 = (UIActionSheet*)view;
    [alert123 dismissWithClickedButtonIndex:-1 animated:NO];
    return YES;
    }
    }
    }
    return NO;

    this is working for UIAlert view but UI action sheet is not working?

Comments are closed.