【问题标题】:How to present iOS UIActionSheet in Swift?如何在 Swift 中呈现 iOS UIActionSheet?
【发布时间】:2015-07-05 10:35:40
【问题描述】:

如何在 iOS 应用中以 Swift 呈现 UIActionSheet?

这是我显示 UIActionSheet 的代码:

@IBAction func downloadSheet(sender: AnyObject) {
    let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .actionSheet) 

    let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        println("Saved")
    })
    
    let deleteAction = UIAlertAction(title: "Delete", style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        println("Deleted")
    })
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
        (alert: UIAlertAction!) -> Void in
        println("Cancelled")
    })
    optionMenu.addAction(deleteAction)
    optionMenu.addAction(saveAction)
    optionMenu.addAction(cancelAction)
    self.presentViewController(optionMenu, animated: true, completion: nil)
}

【问题讨论】:

  • 我认为你的方法很好,因为从 ios8 UIActionSheet 已被弃用。所以你的风格是首选。

标签: ios swift uiactionsheet


【解决方案1】:

斯威夫特 5+ 非常简单和流畅的方式只需调用这个函数和 Boommmmmmm!!!!!!!

let IPAD = UIDevice.current.userInterfaceIdiom == .pad


//Mark:- Choose Image Method
func funcMyActionSheet() {
    var myActionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertController.Style.actionSheet)
    myActionSheet.view.tintColor = UIColor.black
    let galleryAction = UIAlertAction(title: "Gallery".localizableString(language: Defaults.selectedLanguageCode), style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        self.openGallary()
    })
    let cmaeraAction = UIAlertAction(title: "Camera".localizableString(language: Defaults.selectedLanguageCode), style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        self.openCamera()
    })
    let cancelAction = UIAlertAction(title: "Cancel".localizableString(language: Defaults.selectedLanguageCode), style: .cancel, handler: {
        (alert: UIAlertAction!) -> Void in
    })
    

    if IPAD {
        //In iPad Change Rect to position Popover
        myActionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertController.Style.alert)
    }
    myActionSheet.addAction(galleryAction)
    myActionSheet.addAction(cmaeraAction)
    myActionSheet.addAction(cancelAction)
    print("Action Sheet call")
    
    self.present(myActionSheet, animated: true, completion: nil)
}

【讨论】:

    【解决方案2】:

    您可以在 Swift 4 中为 Alert 和 ActionSheet 使用以下代码:

    @IBAction func alert_act(_ sender: Any) {
        do {    
            let alert = UIAlertController(title: "Alert", message: "Would you like to continue learning?", preferredStyle: UIAlertController.Style.alert)
            
            alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.default, handler: nil))
            
            alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: nil))
            
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    @IBAction func action_sheet1(_ sender: Any) {      
        let action_sheet1 = UIAlertController(title: nil, message: "Alert message.", preferredStyle: .actionSheet)
        
        let defaultAction = UIAlertAction(title: "Default", style: .default, handler: nil)
        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        
        action_sheet1.addAction(defaultAction)
        action_sheet1.addAction(deleteAction)
        action_sheet1.addAction(cancelAction)
        
        self.present(action_sheet1, animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码在 Swift 中打开 actionSheet:

      let alert = UIAlertController(title: enter your title, message: "Enter your messgage. ", preferredStyle: UIAlertControllerStyle.Alert)
      
      alert.addTextFieldWithConfigurationHandler(configurationTextField)
      
      alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:{ (UIAlertAction)in
          print("User click Cancel button")
      }))
      
      alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
          print("User click Ok button")
      }))
      
      self.presentViewController(alert, animated: true, completion: {
          print("completion block")
      })
      

      【讨论】:

      • configurationTextField应该是什么?
      • 在这里你会发现: func configurationTextField(textField: UITextField!) { print("configurat租用 TextField") if textField != nil { self.textField = textField! self.textField.delegate = self //保存对UITextField的引用 self.textField.text = "" self.textField.keyboardType = UIKeyboardType.NumberPad self.textField.placeholder = "输入延迟时间以秒为单位" } }
      【解决方案4】:

      适用于 Swift 4、4.2、5 的通用操作表

      如果您喜欢可以从每个ViewController 调用的通用版本,并且在每个项目中都可以尝试这个:

      class Alerts {
       static func showActionsheet(viewController: UIViewController, title: String, message: String, actions: [(String, UIAlertActionStyle)], completion: @escaping (_ index: Int) -> Void) {
          let alertViewController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
          for (index, (title, style)) in actions.enumerated() {
              let alertAction = UIAlertAction(title: title, style: style) { (_) in
                  completion(index)
              }
              alertViewController.addAction(alertAction)
           }
           // iPad Support
           alertViewController.popoverPresentationController?.sourceView = viewController.view
           
           viewController.present(alertViewController, animated: true, completion: nil)
          }
      }
      

      在您的 ViewController 中像这样调用。

          var actions: [(String, UIAlertActionStyle)] = []
          actions.append(("Action 1", UIAlertActionStyle.default))
          actions.append(("Action 2", UIAlertActionStyle.destructive))
          actions.append(("Action 3", UIAlertActionStyle.cancel))
          
          //self = ViewController
          Alerts.showActionsheet(viewController: self, title: "D_My ActionTitle", message: "General Message in Action Sheet", actions: actions) { (index) in
              print("call action \(index)")
              /*
               results
               call action 0
               call action 1
               call action 2
               */
          }
      

      注意:也许您想知道为什么我添加了Action 1/2/3,但得到的结果却像 0,1,2。在for (index, (title, style)) in actions.enumerated() 行中,我得到了操作索引。数组总是从索引 0 开始。所以完成是 0,1,2。

      如果您想设置枚举、id 或其他标识符,我建议您在参数actions 中交出一个对象。

      【讨论】:

      • title: String?message: String? 都可以是可选的
      • @kuzdu - 这很好用,但是在 ipad 上崩溃了。你能更新你的 ipad 答案吗(设置 sourceview 和 sourcerect)可以解决问题
      • 能否请您发布我应该更新的代码?或者更新我的答案,我会接受
      【解决方案5】:

      为 Swift 4/5 更新

      适用于 iOS 11-14

      其他一些答案还可以,但我最终混合并匹配了其中一些答案:

      @IBAction func showAlert(sender: AnyObject) {
          let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet)
          
          alert.addAction(UIAlertAction(title: "Approve", style: .default , handler:{ (UIAlertAction)in
              print("User click Approve button")
          }))
          
          alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
              print("User click Edit button")
          }))
      
          alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
              print("User click Delete button")
          }))
          
          alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
              print("User click Dismiss button")
          }))
      
          
          //uncomment for iPad Support
          //alert.popoverPresentationController?.sourceView = self.view
      
          self.present(alert, animated: true, completion: {
              print("completion block")
          })
      }
      

      享受:)

      【讨论】:

        【解决方案6】:

        有助于填充动态动作列表,并监听选定的动作:

        var categories = ["Science", "History", "Industry", "Agriculture"]
        
        var handlerSelectedCategory: ((Int) -> ())?
        
        func prepareActions(for title: String, index: Int) -> UIAlertAction {
        
            let alertAction = UIAlertAction(title: title, style: .default) { (action) in
                self.handlerSelectedCategory?(index)
            }
        
            return alertAction
        }
        
        @objc func presentActions() {
        
            let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
            // add dymamic options
            for (index, element) in self.categories.enumerated() {
                 optionMenu.addAction(prepareActions(for: element, index: index))
            }
            // cancel
            let cancelAction = UIAlertAction(title: "Cancel", style: .destructive)
            optionMenu.addAction(cancelAction)
        
            handlerSelectedCategory = { index in
                print(index, self.categories[index])
            }
        
            self.present(optionMenu, animated: true, completion: nil)
        }
        

        【讨论】:

          【解决方案7】:

          在 swift4 中向操作表添加警报的代码

             *That means when we click actionsheet values(like edit/ delete..so on) it shows 
             an alert option that is set with Yes or No option*
          
          
             class ViewController: UIViewController
             {
          
          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
          }
          
          @IBAction func action_sheet1(_ sender: Any) {
          
          
          
          
              let action_sheet1 = UIAlertController(title: "Hi Bro", message: "Please Select an Option: ", preferredStyle: .actionSheet)
          
              action_sheet1.addAction(UIAlertAction(title: "Approve", style: .default , handler:{ (alert: UIAlertAction!) -> Void in
                  print("User click Approve button")
          
          
          
                  let alert = UIAlertController(title: "Approve", message: "Would you like to approve the file ", preferredStyle: UIAlertController.Style.alert)
          
                  alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: nil))
          
                  alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.default, handler: nil))
          
          
                  self.present(alert, animated: true, completion: nil)
          
                  }))
          
          
          
          
              action_sheet1.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (alert: UIAlertAction!) -> Void in
                  print("User click Edit button")
          
                  let alert = UIAlertController(title: "Edit", message: "Would you like to edit the file ", preferredStyle: UIAlertController.Style.alert)
          
                  alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: nil))
          
                  alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.default, handler: nil))
          
          
                  self.present(alert, animated: true, completion: nil)
          
                  }))
          
          
          
          
              action_sheet1.addAction(UIAlertAction(title: "Delete", style: .destructive , handler: { (alert: UIAlertAction!) -> Void in
                  print("User click Delete button")
          
          
          
                  let alert = UIAlertController(title: "Delete", message: "Would you like to delete the file permenently?", preferredStyle: UIAlertController.Style.alert)
          
                  alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: nil))
          
                  alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.default, handler: nil))
          
          
                  self.present(alert, animated: true, completion: nil)
          
                  }))
          
          
          
          
          
              action_sheet1.addAction(UIAlertAction(title: "cancel", style: .cancel, handler:{ (alert: UIAlertAction!) -> Void in
                   print("User click cancel button")
          
          
                  let alert = UIAlertController(title: "Cancel", message: "Would you like to cancel?", preferredStyle: UIAlertController.Style.alert)
          
                  alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: nil))
          
                  alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.default, handler: nil))
          
          
                  self.present(alert, animated: true, completion: nil)
          
                  }))
          
          
          
                  self.present(action_sheet1, animated: true, completion: {
                  print("completion block")
              })
          }
          
          
          }
          

          【讨论】:

            【解决方案8】:

            为 Swift 3.x、Swift 4.x、Swift 5.x 更新

            // create an actionSheet
            let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
            
            // create an action
            let firstAction: UIAlertAction = UIAlertAction(title: "First Action", style: .default) { action -> Void in
            
                print("First Action pressed")
            }
            
            let secondAction: UIAlertAction = UIAlertAction(title: "Second Action", style: .default) { action -> Void in
            
                print("Second Action pressed")
            }
            
            let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in }
            
            // add actions
            actionSheetController.addAction(firstAction)
            actionSheetController.addAction(secondAction)
            actionSheetController.addAction(cancelAction)
            
            
            // present an actionSheet...
            // present(actionSheetController, animated: true, completion: nil)   // doesn't work for iPad
            
            actionSheetController.popoverPresentationController?.sourceView = yourSourceViewName // works for both iPhone & iPad
            
            present(actionSheetController, animated: true) {
                print("option menu presented")
            }
            

            【讨论】:

            • 也感谢您提供的屏幕截图。如果您想要 RED 文本,只需将样式更改为 .破坏性
            【解决方案9】:

            iOS10 中的Action Sheet 与Swift3.0。请点击此链接。

             @IBAction func ShowActionSheet(_ sender: UIButton) {
                // Create An UIAlertController with Action Sheet
            
                let optionMenuController = UIAlertController(title: nil, message: "Choose Option from Action Sheet", preferredStyle: .actionSheet)
            
                // Create UIAlertAction for UIAlertController
            
                let addAction = UIAlertAction(title: "Add", style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    print("File has been Add")
                })
                let saveAction = UIAlertAction(title: "Edit", style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    print("File has been Edit")
                })
            
                let deleteAction = UIAlertAction(title: "Delete", style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    print("File has been Delete")
                })
                let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
                    (alert: UIAlertAction!) -> Void in
                    print("Cancel")
                })
            
                // Add UIAlertAction in UIAlertController
            
                optionMenuController.addAction(addAction)
                optionMenuController.addAction(saveAction)
                optionMenuController.addAction(deleteAction)
                optionMenuController.addAction(cancelAction)
            
                // Present UIAlertController with Action Sheet
            
                self.present(optionMenuController, animated: true, completion: nil)
            
            }
            

            【讨论】:

              【解决方案10】:

              SWIFT 4

              适用于 IPHONE 和 IPAD。还允许旋转

              风景

              肖像

              代码(已测试)

              let alert = UIAlertController()
              
                      let width: Int = Int(UIScreen.main.bounds.width - 100)
              
                      let viewAction    =     UIAlertAction(title: "View", style: .default , handler:{ (UIAlertAction)in
                                                  let storyboard      =   UIStoryboard(name: "Main", bundle: nil)
                                                  let orderDetailVC   =   storyboard.instantiateViewController(withIdentifier: "orderDetail") as! OrderDetailTableViewController
              
                                                  orderDetailVC.orderId     =   self.myDraftOrders[indexPath.row]["id"].intValue
              
                                                  self.navigationController?.pushViewController(orderDetailVC, animated: true)
                                              })
              
                      viewAction.setValue(appGreenColor, forKey: "titleTextColor")
              
                      alert.addAction(viewAction)
              
                      let modifyAction    =   UIAlertAction(title: "Modify", style: .default, handler:{ (UIAlertAction)in
                                                  showAlert("Coming soon...")
                                              })
              
                      modifyAction.setValue(appCyanColor, forKey: "titleTextColor")
              
                      alert.addAction(modifyAction)
              
                      let copyAction      =   UIAlertAction(title: "Copy", style: .default, handler:{ (UIAlertAction)in
              
                                                  self.copyOrder(orderId: self.myDraftOrders[indexPath.row]["id"].intValue)
              
                                              })
              
                      copyAction.setValue(appBlueColor, forKey: "titleTextColor")
              
                      alert.addAction(copyAction)
              
                      alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
              
                          self.deleteOrder(orderId: self.myDraftOrders[indexPath.row]["id"].intValue, indexPath: indexPath)
              
                      }))
              
                      alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction)in
                          print("User click Dismiss button")
              
                      }))
              
                      let popover = alert.popoverPresentationController
              
                      popover?.delegate = self
              
                      let cellT = tableView.cellForRow(at: indexPath)
              
                      popover?.sourceView =   cellT
                      popover?.sourceRect =   CGRect(x: width, y: 25, width: 100, height: 50)
                      present(alert, animated: true)
              

              【讨论】:

                【解决方案11】:

                swift4(已测试)

                let alertController = UIAlertController(title: "Select Photo", message: "Select atleast one photo", preferredStyle: .actionSheet)
                    let action1 = UIAlertAction(title: "From Photo", style: .default) { (action) in
                        print("Default is pressed.....")
                    }
                    let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
                        print("Cancel is pressed......")
                    }
                    let action3 = UIAlertAction(title: "Click new", style: .default) { (action) in
                        print("Destructive is pressed....")
                
                    }
                    alertController.addAction(action1)
                    alertController.addAction(action2)
                    alertController.addAction(action3)
                    self.present(alertController, animated: true, completion: nil)
                
                }
                

                【讨论】:

                  【解决方案12】:

                  您的方法很好,但您可以轻松添加UIActionSheet

                  你可以在 UIViewController` 中添加UIActionSheetDelegate

                  class ViewController: UIViewController ,UIActionSheetDelegate
                  

                  设置你的方法,

                  @IBAction func downloadSheet(sender: AnyObject)
                  {
                  
                      let actionSheet = UIActionSheet(title: "Choose Option", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Save", "Delete")
                  
                      actionSheet.showInView(self.view)
                  }
                  

                  您可以在单击时获取按钮索引

                  func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
                  {
                      println("\(buttonIndex)")
                      switch (buttonIndex){
                  
                      case 0:
                          println("Cancel")
                      case 1:
                          println("Save")
                      case 2:
                          println("Delete")
                      default:
                          println("Default")
                          //Some code here..
                  
                      }
                  }
                  

                  更新 1:适用于 iOS8+

                      //Create the AlertController and add Its action like button in Actionsheet
                      let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .ActionSheet)
                  
                      let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in
                          print("Cancel")
                      }
                      actionSheetControllerIOS8.addAction(cancelActionButton)
                  
                      let saveActionButton = UIAlertAction(title: "Save", style: .default)
                          { _ in
                             print("Save")
                      }
                      actionSheetControllerIOS8.addAction(saveActionButton)
                  
                      let deleteActionButton = UIAlertAction(title: "Delete", style: .default)
                          { _ in
                              print("Delete")
                      }
                      actionSheetControllerIOS8.addAction(deleteActionButton)
                      self.present(actionSheetControllerIOS8, animated: true, completion: nil)
                  

                  【讨论】:

                  【解决方案13】:

                  斯威夫特 3 用于在 iPad 上从 UIBarButtonItem 显示 UIAlertController

                          let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet)
                  
                      alert.addAction(UIAlertAction(title: "Approve", style: .default , handler:{ (UIAlertAction)in
                          print("User click Approve button")
                      }))
                  
                      alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
                          print("User click Edit button")
                      }))
                  
                      alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
                          print("User click Delete button")
                      }))
                  
                      alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.cancel, handler:{ (UIAlertAction)in
                          print("User click Dismiss button")
                      }))
                  
                      if let presenter = alert.popoverPresentationController {
                          presenter.barButtonItem = sender
                      }
                  
                      self.present(alert, animated: true, completion: {
                          print("completion block")
                      })
                  

                  【讨论】:

                    【解决方案14】:

                    UIActionSheet 在 iOS 8 中已弃用。

                    我正在使用以下内容:

                    // Create the AlertController
                    let actionSheetController = UIAlertController(title: "Please select", message: "How you would like to utilize the app?", preferredStyle: .ActionSheet)
                    
                    // Create and add the Cancel action
                    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
                        // Just dismiss the action sheet
                    }
                    actionSheetController.addAction(cancelAction)
                    
                    // Create and add first option action
                    let takePictureAction = UIAlertAction(title: "Consumer", style: .Default) { action -> Void in
                        self.performSegueWithIdentifier("segue_setup_customer", sender: self)
                    }
                    actionSheetController.addAction(takePictureAction)
                    
                    // Create and add a second option action
                    let choosePictureAction = UIAlertAction(title: "Service provider", style: .Default) { action -> Void in
                        self.performSegueWithIdentifier("segue_setup_provider", sender: self)
                    }
                    actionSheetController.addAction(choosePictureAction)
                    
                    // We need to provide a popover sourceView when using it on iPad
                    actionSheetController.popoverPresentationController?.sourceView = sender as UIView
                    
                    // Present the AlertController
                    self.presentViewController(actionSheetController, animated: true, completion: nil)
                    

                    【讨论】:

                      【解决方案15】:

                      Swift 3 更新:

                      // Create the AlertController and add its actions like button in ActionSheet
                      let actionSheetController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .actionSheet)
                      
                      let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
                          print("Cancel")
                      }
                      actionSheetController.addAction(cancelActionButton)
                      
                      let saveActionButton = UIAlertAction(title: "Save", style: .default) { action -> Void in
                          print("Save")
                      }
                      actionSheetController.addAction(saveActionButton)
                      
                      let deleteActionButton = UIAlertAction(title: "Delete", style: .default) { action -> Void in
                          print("Delete")
                      }
                      actionSheetController.addAction(deleteActionButton)
                      self.present(actionSheetController, animated: true, completion: nil)
                      

                      【讨论】:

                        【解决方案16】:

                        旧方式:UIActionSheet

                        let actionSheet = UIActionSheet(title: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Destroy", otherButtonTitles: "OK")
                        actionSheet.actionSheetStyle = .Default
                        actionSheet.showInView(self.view)
                        
                        // MARK: UIActionSheetDelegate
                        
                        func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex  buttonIndex: Int) {
                        switch buttonIndex {
                            ...
                           }
                        }
                        

                        新方式:UIAlertController

                        let alertController = UIAlertController(title: nil, message: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", preferredStyle: .ActionSheet)
                        
                        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
                          // ...
                         }
                         alertController.addAction(cancelAction)
                        
                        let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
                           // ...
                        }
                        alertController.addAction(OKAction)
                        
                        let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in
                        println(action)
                        }
                        alertController.addAction(destroyAction)
                        
                        self.presentViewController(alertController, animated: true) {
                        // ...
                        }
                        

                        【讨论】:

                          【解决方案17】:

                          斯威夫特:

                          下面给出的示例代码适用于 iPhone 和 iPad。

                           guard let viewRect = sender as? UIView else {
                                          return
                                      }
                          
                                  let cameraSettingsAlert = UIAlertController(title: NSLocalizedString("Please choose a course", comment: ""), message: NSLocalizedString("", comment: ""), preferredStyle: .ActionSheet)
                                  cameraSettingsAlert.modalPresentationStyle = .Popover
                          
                                  let photoResolutionAction = UIAlertAction(title: NSLocalizedString("Photo Resolution", comment: ""), style: .Default) { action in
                          
                                  }
                                  let cameraOrientationAction = UIAlertAction(title: NSLocalizedString("Camera Orientation", comment: ""), style: .Default) { action in
                          
                                  }
                                  let flashModeAction = UIAlertAction(title: NSLocalizedString("Flash Mode", comment: ""), style: .Default) { action in
                          
                                  }
                                  let timeStampOnPhotoAction = UIAlertAction(title: NSLocalizedString("Time Stamp on Photo", comment: ""), style: .Default) { action in
                          
                                  }
                                  let cancel = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel) { action in
                          
                                  }
                                  cameraSettingsAlert.addAction(cancel)
                                  cameraSettingsAlert.addAction(cameraOrientationAction)
                                  cameraSettingsAlert.addAction(flashModeAction)
                                  cameraSettingsAlert.addAction(timeStampOnPhotoAction)
                                  cameraSettingsAlert.addAction(photoResolutionAction)
                          
                                  if let presenter = cameraSettingsAlert.popoverPresentationController {
                                      presenter.sourceView = viewRect;
                                      presenter.sourceRect = viewRect.bounds;
                                  }
                                  presentViewController(cameraSettingsAlert, animated: true, completion: nil)
                          

                          【讨论】:

                            猜你喜欢
                            • 2012-09-27
                            • 1970-01-01
                            • 2015-10-28
                            • 1970-01-01
                            • 1970-01-01
                            • 2013-03-06
                            • 2018-10-21
                            • 2015-10-20
                            • 1970-01-01
                            相关资源
                            最近更新 更多