我创建了一个操作表,但问题是没有调用委托方法
myActionSheet = UIActionSheet()
myActionSheet.addButtonWithTitle("Add event")
myActionSheet.addButtonWithTitle("close")
myActionSheet.cancelButtonIndex = 1
myActionSheet.showInView(self.view)
/// UIActionSheetDelegate
func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
if(myActionSheet.tag == 1){
if (buttonIndex == 0){
println("the index is 0")
}
}
}
我使用了另一种适用于iOS 8的方法但不适用于iOS 7:
var ActionSheet = UIAlertController(title: "Add View", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
ActionSheet.addAction(UIAlertAction(title: "Add event", style: UIAlertActionStyle.Default, handler:nil))
self.presentViewController(ActionSheet, animated: true, completion: nil)
有什么想法解决这个问题吗?
答案 0 :(得分:20)
快速语言的UIActionSheet: -
带有cancelButton和destructiveButton的行动表
设置UIActionSheetDelegate。
let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done")
actionSheet.showInView(self.view)
带有cancelButton,destructiveButton和otherButton
的操作表 let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
actionSheet.showInView(self.view)
创建操作表功能
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..
}
答案 1 :(得分:14)
UIActionSheet
已被弃用,如果您不必支持以下版本,我建议您使用UIAlertController
:
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)
}
以下是呈现的内容:
答案 2 :(得分:8)
您永远不会设置操作表的委托:
myActionSheet = UIActionSheet()
myActionSheet.delegate = self
答案 3 :(得分:0)
针对Swift 3进行了更新:
如果您想在按钮上单击显示/打开UIActionSheet,请在您的ViewController中使用以下简单和更新的代码:
//方法定义:
func showPaymentModeActionSheet() {
// 1
let optionMenu = UIAlertController(title: nil, message: "Choose Payment Mode", preferredStyle: .actionSheet)
// 2
let fullAction = UIAlertAction(title: "FULL", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.mPaymentModeTextField.text = "FULL"
})
let addvanceAction = UIAlertAction(title: "ADVANCE", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.mPaymentModeTextField.text = "ADVANCE"
})
//
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
(alert: UIAlertAction!) -> Void in
})
// 4
optionMenu.addAction(fullAction)
optionMenu.addAction(addvanceAction)
optionMenu.addAction(cancelAction)
// 5
self.present(optionMenu, animated: true, completion: nil)
}
//方法调用:
@IBAction func actionOnPaymentModeButton(_ sender: Any) {
// open action sheet
showPaymentModeActionSheet()
}
答案 4 :(得分:0)
在 Swift 4 / 5
中显示操作表 @IBAction func showActionSheet(sender: AnyObject) {
let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet)
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: "Cancel", style: .cancel, handler:{ (UIAlertAction)in
print("User click Dismiss button")
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}