UIActionSheet with swift
UIActionSheet
is deprecated since iOS8, I would recommend using UIAlertController
if you don't have to support version below:
private func presentSettingsActionSheet() {
let settingsActionSheet: UIAlertController = UIAlertController(title:nil, message:nil, preferredStyle:UIAlertControllerStyle.ActionSheet)
settingsActionSheet.addAction(UIAlertAction(title:"Send Feedback", style:UIAlertActionStyle.Default, handler:{ action in
self.presentFeedbackForm()
}))
settingsActionSheet.addAction(UIAlertAction(title:"Tell Me a Joke!", style:UIAlertActionStyle.Default, handler:{ action in
self.presentRandomJoke()
}))
settingsActionSheet.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.Cancel, handler:nil))
presentViewController(settingsActionSheet, animated:true, completion:nil)
}
Here is what it looks like presented:
You never set the action sheet's delegate:
myActionSheet = UIActionSheet()
myActionSheet.delegate = self
UIActionSheet in swift language :-
Action Sheet with cancelButton and destructiveButton
set the UIActionSheetDelegate.
let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done")
actionSheet.showInView(self.view)
Action Sheet with cancelButton , destructiveButton and otherButton
let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
actionSheet.showInView(self.view)
create the Action sheet function
func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int)
{
switch buttonIndex{
case 0:
NSLog("Done");
break;
case 1:
NSLog("Cancel");
break;
case 2:
NSLog("Yes");
break;
case 3:
NSLog("No");
break;
default:
NSLog("Default");
break;
//Some code here..
}