UIActionSheet iOS Swift

如何在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) } 

我希望我的代码清晰明了…欢迎更好的build议。

你的方法很好,但你可以用其他方式轻松添加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) } 

点击时你可以得到你的button索引

 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) 

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) 

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) 

适用于iOS 9某些其他的答案是好的,但我最终混合和匹配其中的一些,而不是拿出这个:

 @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: UIAlertActionStyle.Cancel, handler:{ (UIAlertAction)in print("User click Dismiss button") })) self.presentViewController(alert, animated: true, completion: { print("completion block") }) } 

享受🙂

更新了Swift 3

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

在这里输入图像说明

旧的方式: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) { // ... } 

Swift:

下面给出的示例代码适用于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) 

使用Swift3.0的iOS10操作手册。 按照这个链接。

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

http://www.problemstucks.com/Action-Sheet-with-Swift3.0.html

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

你可以在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") })