ios7 - UIActionSheet 与 swift

标签 ios7 swift uiactionsheet ios8

我创建了一个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/

相关文章:

ios - 禁用 MFMessageComposeViewController 的自定义外观

objective-c - 应用程序动画从纵向到横向

ios - 如何在 ios 中向我的 ImageView 添加角标(Badge)?

ios - 操作表需要 10 秒以上才能出现

ios - 在 iOS7 中创建没有图像的圆形按钮

ios - 在 ios 中的表格单元格中单击按钮更改图像

ios - CGAffineTransform 不在动画 block 内设置动画

ios - 我的背景音乐被播放了两次(同时)

ios - 如何使用操作表执行文本字段的重置?

ios - UIAlertController/ActionSheet 在 iPad 上崩溃,如何传递发件人?