Ios7 带swift的行动单

Ios7 带swift的行动单,ios7,swift,uiactionsheet,ios8,Ios7,Swift,Uiactionsheet,Ios8,我创建了一个操作表,但问题是没有调用委托方法 myActionSheet = UIActionSheet() myActionSheet.addButtonWithTitle("Add event") myActionSheet.addButtonWithTitle("close") myActionSheet.cancelButtonIndex = 1 myActionSheet.showInView(self.view) /

我创建了一个操作表,但问题是没有调用委托方法

 myActionSheet = UIActionSheet()
        myActionSheet.addButtonWithTitle("Add event")
        myActionSheet.addButtonWithTitle("close")
        myActionSheet.cancelButtonIndex = 1
        myActionSheet.showInView(self.view)
///代表

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)

有什么办法解决这个问题吗?

您从未设置过操作表的委托:

myActionSheet = UIActionSheet()
myActionSheet.delegate = self

UIActionSheet以swift语言编写:-

带有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..

 }

UIActionSheet
自iOS8以来已被弃用,如果您不必支持以下版本,我建议您使用
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)
}
下面是它的外观:

为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()
}

在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")
    })
}

操作表和警报视图在iOS 8中被降级,并且引入了alertController。是的,它已被弃用,但如果您支持iOS 7应用程序,则alertController将无法工作。因此,最好检查iOS版本并调用iOS 8和iOS 7的相应代码看看这个答案-我编写了一个Swift版本,不依赖硬编码按钮索引来获得另一个答案: