21

I'd like to create a pop-up menu similar to the one found in the mail app when you want to reply to a message. I've seen this in more than one application so I wasn't sure if there was something built into the framework for it or some example code out there.

UIActionSheet example

reixa
  • 6,903
  • 6
  • 49
  • 68
LeeMobile
  • 3,775
  • 6
  • 31
  • 32
  • best resource i found here http://code.tutsplus.com/tutorials/uiactionsheet-and-uiactionsheetdelegate--mobile-11590 – Zar E Ahmer Aug 30 '14 at 07:14

7 Answers7

25

Creating an Action Sheet in Swift

Code has been tested with Swift 5

enter image description here

Since iOS 8, UIAlertController combined with UIAlertControllerStyle.ActionSheet is used. UIActionSheet is deprecated.

Here is the code to produce the Action Sheet in the above image:

class ViewController: UIViewController {
    
    @IBOutlet weak var showActionSheetButton: UIButton!
    
    @IBAction func showActionSheetButtonTapped(sender: UIButton) {
        
        // Create the action sheet
        let myActionSheet = UIAlertController(title: "Color", message: "What color would you like?", preferredStyle: UIAlertController.Style.actionSheet)
        
        // blue action button
        let blueAction = UIAlertAction(title: "Blue", style: UIAlertAction.Style.default) { (action) in
            print("Blue action button tapped")
        }
        
        // red action button
        let redAction = UIAlertAction(title: "Red", style: UIAlertAction.Style.default) { (action) in
            print("Red action button tapped")
        }
        
        // yellow action button
        let yellowAction = UIAlertAction(title: "Yellow", style: UIAlertAction.Style.default) { (action) in
            print("Yellow action button tapped")
        }
        
        // cancel action button
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) { (action) in
            print("Cancel action button tapped")
        }
        
        // add action buttons to action sheet
        myActionSheet.addAction(blueAction)
        myActionSheet.addAction(redAction)
        myActionSheet.addAction(yellowAction)
        myActionSheet.addAction(cancelAction)
        
        // present the action sheet
        self.present(myActionSheet, animated: true, completion: nil)
    }
}

Still need help? Watch this video tutorial. That's how I learned it.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Hi @Suragch, I try your code. It works for me in iPad and iPod. But, Cancel button does not appear in iPad. I don't know why. It shows in iPod and I use not button. I use image and add tap function on it. So, my code is alertController.popoverPresentationController?.sourceView = self.imgPlay and alertController.popoverPresentationController?.sourceRect = self.imgPlay.bounds . – May Phyu Mar 09 '17 at 07:03
  • It works very fine, but can we customise buttons text @Suragch – Mansuu.... Aug 10 '17 at 04:56
20

It is a UIAlertController on iOS 8+, and a UIActionSheet on earlier versions.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rémy
  • 1,091
  • 9
  • 17
14

Check out the UICatalog example on Apple's website. The "Alerts" section has examples of how to use UIActionSheet to accomplish what you're trying to do.

Mike
  • 323
  • 2
  • 7
9

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 as of iOS 8.x.

https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
5

This is how you'd do it in Objective-C on iOS 8+:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions"
                                                                           message:@"Select mode of transportation:"
                                                                    preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *drivingAction = [UIAlertAction actionWithTitle:@"Driving" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // this block runs when the driving option is selected
    }];
    UIAlertAction *walkingAction = [UIAlertAction actionWithTitle:@"Walking" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // this block runs when the walking option is selected
    }];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:drivingAction];
    [alert addAction:walkingAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
Stunner
  • 12,025
  • 12
  • 86
  • 145
2

To everybody who is looking for a solution in Swift:

  1. Adopt UIActionSheetDelegate protocol

  2. Create and show the ActinSheet:

    let sheet: UIActionSheet = UIActionSheet()
    
    sheet.addButtonWithTitle("button 1")
    sheet.addButtonWithTitle("button 2")
    sheet.addButtonWithTitle("button 3")
    sheet.addButtonWithTitle("Cancel")
    sheet.cancelButtonIndex = sheet.numberOfButtons - 1
    sheet.delegate = self
    sheet.showInView(self.view)
    
  3. The delegate function:

    func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
    switch buttonIndex{
        case 0:
            NSLog("button1");
        case 1:
            NSLog("button2");
        case 2:
            NSLog("button3");
        case actionSheet.cancelButtonIndex:
            NSLog("cancel");
            break;
        default:
            NSLog("blub");
            break;
      }
    }
    
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
Apfelsaft
  • 5,766
  • 4
  • 28
  • 37
0

I tried to add ActionSheet on my view. So I've been trying to find perfect solution but some answers made me confused. Because most of questions about Action sheet were written so long time ago.Also it has not been updated. Anyway...I will write old version ActionSheet and updated version ActionSheet. I hope my answer is able to make your brain peaceful.

----------Updated Version---------

    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"writeMessageOrsetAsNil" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction* actionSheet01 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction * action) {  NSLog(@"OK click");}];

    UIAlertAction* actionSheet02 = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault
                                                                 handler:^(UIAlertAction * action) {NSLog(@"OK click");}];

    UIAlertAction* actionSheet03 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                                                 handler:^(UIAlertAction * action) {
                                                                     NSLog(@"Cancel click");}];

    [browserAlertController addAction:actionSheet01];
    [browserAlertController addAction:actionSheet02];
    [browserAlertController addAction:actionSheet03];

    [self presentViewController:browserAlertController animated:YES completion:nil];

-------Old Version------

UIActionSheet *actionSheet= [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@“OK”, @“NO”,@“Cancel”,
                        nil];
  actionSheet.tag = 100;
  [actionSheet showInView:self.view];

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

  if( actionSheet.tag  == 100){
        switch (buttonIndex) {
            case 0:
                [self doSomething];
                break;
            case 1:
                [self doAnything];
                break;
            case 2:
                [self doNothing];
                break;
             default:
                break;
        }
        break;
    }

}
User18474728
  • 363
  • 2
  • 11