How to present UIActionSheet iOS Swift?
Updated for Swift 4/5
Works for iOS 11-14
Some of the other answers are okay but I ended up mixing and matching a few of them to rather come up with this :
@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") })}
Enjoy :)
Your Approach is fine, but you can add UIActionSheet
with other way with ease.
You can add UIActionSheetDelegate
in UIViewController` like
class ViewController: UIViewController ,UIActionSheetDelegate
Set you method like,
@IBAction func downloadSheet(sender: AnyObject){ let actionSheet = UIActionSheet(title: "Choose Option", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Save", "Delete") actionSheet.showInView(self.view)}
You can get your button index when it clicked like
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.. }}
Update 1: for 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
is deprecated in iOS 8.
I am using following:
// Create the AlertControllerlet actionSheetController = UIAlertController(title: "Please select", message: "How you would like to utilize the app?", preferredStyle: .ActionSheet)// Create and add the Cancel actionlet cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in // Just dismiss the action sheet}actionSheetController.addAction(cancelAction)// Create and add first option actionlet 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 actionlet 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 iPadactionSheetController.popoverPresentationController?.sourceView = sender as UIView// Present the AlertControllerself.presentViewController(actionSheetController, animated: true, completion: nil)