How To Do UIActionSheet in iOS Swift? Here is my code for coding 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)
}
I hope my Code is clear... I am welcome better suggestion for this code.
Generetic Action Sheet working for Swift 4 and 4.2
If you like a generic version that you can call from every
ViewController
and in every project try this one:Call like this in your ViewController.
Attention: Maybe you're wondering why I add
Action 1/2/3
but got results like 0,1,2. In the linefor (index, (title, style)) in actions.enumerated()
I get the index of actions. Arrays always begin with the index 0. So the completion is 0,1,2.If you like to set a enum, an id or another identifier I would recommend to hand over an object in parameter
actions
.Action Sheet in iOS10 with Swift3.0. Follow this link.
http://www.problemstucks.com/Action-Sheet-with-Swift3.0.html
Swift 3 For displaying UIAlertController from UIBarButtonItem on iPad
You can use following code for open actionSheet in Swift
swift4 (tested)
UIActionSheet
is deprecated in iOS 8.I am using following: