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.
Updated for Swift 4
Works for iOS 11
Some of the other answers are okay but I ended up mixing and matching a few of them to rather come up with this :
Enjoy :)
Updated for Swift 3
SWIFT 4
WORKS ON IPHONE AND IPAD BOTH. ALSO ALLOWS ROTATION
PORTRAIT
Code (tested)
Update for Swift 3:
Swift :
The sample code given below works both on iPhone and iPad.
Your Approach is fine, but you can add
UIActionSheet
with other way with ease.You can add
UIActionSheetDelegate
in UIViewController` likeSet you method like,
You can get your button index when it clicked like
Update 1: for iOS8+