我创建了一个action sheet,但问题是委托(delegate)方法没有被调用
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)
有解决问题的办法吗?
最佳答案
快速语言的 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..
}
关于ios7 - UIActionSheet 与 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172605/