58

I would like to create this kind of menu, of course with other menu buttons. Is there any default viewcontroller representing it, or do I have to get images and create this by myself.

enter image description here

Tim
  • 8,206
  • 3
  • 40
  • 64
mbutan
  • 1,309
  • 2
  • 11
  • 22

4 Answers4

169

You need to use a UIActionSheet.

First you need to add UIActionSheetDelegate to your ViewController.h file.

Then you can reference an actionsheet with:

  UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
                        @"Share on Facebook",
                        @"Share on Twitter",
                        @"Share via E-mail",
                        @"Save to Camera Roll",
                        @"Rate this App",
                        nil];
   popup.tag = 1;
  [popup showInView:self.view];

Then you have to handle each of the calls.

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {

  switch (popup.tag) {
    case 1: {
        switch (buttonIndex) {
            case 0:
                [self FBShare];
                break;
            case 1:
                [self TwitterShare];
                break;
            case 2:
                [self emailContent];
                break;
            case 3:
                [self saveContent];
                break;
            case 4:
                [self rateAppYes];
                break;
            default:
                break;
        }
        break;
    }
    default:
        break;
 }
}

This has been deprecated for iOS 8.x https://developer.apple.com/documentation/uikit/uialertcontroller#//apple_ref/occ/cl/UIAlertController

Cœur
  • 32,421
  • 21
  • 173
  • 232
Apollo SOFTWARE
  • 11,420
  • 4
  • 41
  • 61
  • 5
    +! for being the only one to call it a `UIActionSheet` (as well as providing a good answer). – rmaddy Jun 20 '13 at 20:37
  • 1
    I was literally working on my app as I saw the question! It comes from my code verbatim ;) – Apollo SOFTWARE Jun 20 '13 at 20:47
  • 1
    it is also working without adding UIActionSheetDelegate .. I want to know how... – AndroidGeek Aug 30 '14 at 07:12
  • 2
    with iOS 8 there is a new UIAlertController https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController – Apollo SOFTWARE Sep 24 '14 at 15:17
  • re the Sep 24, '14 comment and edit, there is a new UIAlertController and UIActionSheet is deprecated as of iOS 8, but to say that it has "changed" is not fully accurate. It might be better to include the link to the class reference but say that it is deprecated rather than changed. – Tony Adams Mar 19 '15 at 20:12
  • NOTE: `[popup showInView:[UIApplication sharedApplication].keyWindow];` does not work alot of the time on iPad apps, show it in your current UIView instead. – Albert Renshaw Jul 16 '15 at 22:02
  • @AlbertRenshaw you are correct. You can use self or comparable and show in current view. – Apollo SOFTWARE Jul 17 '15 at 04:48
11

Take a look at the UIActionSheet documentation.

NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title
NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles
NSString *other1 = @"Other Button 1";
NSString *other2 = @"Other Button 2";
NSString *other3 = @"Other Button 3";
NSString *cancelTitle = @"Cancel Button";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:actionSheetTitle
                              delegate:self
                              cancelButtonTitle:cancelTitle
                              destructiveButtonTitle:destructiveTitle
                              otherButtonTitles:other1, other2, other3, nil];
[actionSheet showInView:self.view];
Rob
  • 4,809
  • 12
  • 48
  • 49
5

It is called an UIActionSheet: You create one like so:

    NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title
NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles
NSString *other1 = @"Other Button 1";
NSString *other2 = @"Other Button 2";
NSString *other3 = @"Other Button 3";
NSString *cancelTitle = @"Cancel Button";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:actionSheetTitle
                              delegate:self
                              cancelButtonTitle:cancelTitle
                              destructiveButtonTitle:destructiveTitle
                              otherButtonTitles:other1, other2, other3, nil];
[actionSheet showInView:self.view];

Implement the UISctionSheetDelegate to respond to button action.

Take a look at this tutorial for more info: http://mobile.tutsplus.com/tutorials/iphone/uiactionsheet_uiactionsheetdelegate (Code is from this tutorial)

3

What you are looking for is called an actionsheet. Read more about it here http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIActionSheet_Class/Reference/Reference.html

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Ben
  • 589
  • 7
  • 19