创建类似于邮件应用程序菜单的iPhone弹出式菜单

21

我想创建一个类似于邮件应用中回复消息时出现的弹出菜单。我在多个应用程序中看到过这种类型的菜单,因此不确定是否有内置的框架或示例代码可用。

UIActionSheet example


我在这里找到的最佳资源是http://code.tutsplus.com/tutorials/uiactionsheet-and-uiactionsheetdelegate--mobile-11590。 - Zar E Ahmer
7个回答

25

在Swift中创建操作表(Action Sheet)

本代码已经过Swift 5测试

图片描述

自iOS 8以来,UIAlertController结合UIAlertControllerStyle.ActionSheet被使用,UIActionSheet已被弃用。

以下是生成上述图像中的操作表所需的代码:

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)
    }
}

还需要帮助吗?看看这个视频教程吧。那是我学会它的方法。


嗨@Suragch,我试了一下你的代码,在iPad和iPod上都可以正常运行。但是,在iPad上取消按钮没有出现。我不知道为什么。在iPod上它显示了,而我使用image并在其上添加tap函数。所以,我的代码是alertController.popoverPresentationController?.sourceView = self.imgPlay和alertController.popoverPresentationController?.sourceRect = self.imgPlay.bounds。 - May Phyu
它的运行非常好,但是我们能否自定义@Suragch按钮文本? - Mansuu....

20

14

请查看苹果网站上的UICatalog示例。"Alerts"部分展示了如何使用UIActionSheet实现你要做的事情。


9

您需要使用UIActionSheet。

首先您需要在ViewController的.h文件中添加UIActionSheetDelegate。

然后,您可以通过以下方式引用操作表:

  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];

然后你需要处理每个调用。
- (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;
 }
}

这已经在iOS 8.x中被弃用。

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


5
这是在iOS 8+上使用Objective-C的方法:
    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];

2

对于所有正在寻找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;
      }
    }
    

0

我尝试在我的视图上添加操作表。因此,我一直在寻找完美的解决方案,但一些答案让我感到困惑。因为大多数有关操作表的问题都是很久以前写的,并且它们没有得到更新。

无论如何...我将编写旧版本的操作表和更新版本的操作表。

我希望我的答案能使您的大脑平静下来。

----------更新版---------

    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];

-------旧版本------

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;
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接