【问题标题】:Creating iPhone Pop-up Menu Similar to Mail App Menu创建类似于邮件应用程序菜单的 iPhone 弹出菜单
【发布时间】:2010-10-29 00:13:27
【问题描述】:

当您想要回复消息时,我想创建一个类似于邮件应用程序中的弹出菜单。我在不止一个应用程序中看到过这种情况,所以我不确定框架中是否有内置的东西或一些示例代码。

【问题讨论】:

标签: ios objective-c cocoa-touch uiview


【解决方案1】:

在 Swift 中创建操作表

代码已经用 Swift 5 测试过

从 iOS 8 开始,UIAlertControllerUIAlertControllerStyle.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 中,我使用 not 按钮。我使用图像并在其上添加点击功能。所以,我的代码是 alertController.popoverPresentationController?.sourceView = self.imgPlay 和 alertController.popoverPresentationController?.sourceRect = self.imgPlay.bounds 。
  • 它工作得很好,但是我们可以自定义按钮文本@Suragch
【解决方案2】:

我尝试在我的视图中添加 ActionSheet。所以我一直在努力寻找完美的解决方案,但有些答案让我感到困惑。因为关于Action sheet的大部分问题都是很久以前写的。而且它还没有更新。 无论如何...我将编写旧版本的 ActionSheet 和更新版本的 ActionSheet。 希望我的回答能让你的大脑平静下来。

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

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

}

【讨论】:

    【解决方案3】:

    在 iOS 8+ 上是 UIAlertController,在早期版本上是 UIActionSheet

    【讨论】:

      【解决方案4】:

      致所有在 Swift 中寻找解决方案的人:

      1. 采用UIActionSheetDelegate协议

      2. 创建并显示 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. 委托函数:

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

      【讨论】:

        【解决方案5】:

        您需要使用 UIActionSheet。

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

        然后您可以使用以下方式引用操作表:

          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

        【讨论】:

          【解决方案6】:

          这就是你在 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];
          

          【讨论】:

            【解决方案7】:

            查看 Apple 网站上的 UICatalog 示例。 “警报”部分提供了有关如何使用 UIActionSheet 来完成您正在尝试执行的操作的示例。

            【讨论】:

              猜你喜欢
              • 2011-06-13
              • 1970-01-01
              • 2014-10-03
              • 2016-04-15
              • 1970-01-01
              • 2016-02-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多