UIActionSheet与swift

mot*_*teb 10 uiactionsheet ios7 swift ios8

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

 myActionSheet = UIActionSheet()
        myActionSheet.addButtonWithTitle("Add event")
        myActionSheet.addButtonWithTitle("close")
        myActionSheet.cancelButtonIndex = 1
        myActionSheet.showInView(self.view)
Run Code Online (Sandbox Code Playgroud)

/// UIActionSheetDelegate

func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
        if(myActionSheet.tag == 1){

            if (buttonIndex == 0){
                println("the index is 0")
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

我使用了另一种适用于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)
Run Code Online (Sandbox Code Playgroud)

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

Ani*_*mar 20

快速语言的UIActionSheet: -

带有cancelButton和destructiveButton的操作表

设置UIActionSheetDelegate.

        let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done")
        actionSheet.showInView(self.view)
Run Code Online (Sandbox Code Playgroud)

使用cancelButton,destructiveButton和otherButton的操作表

        let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
        actionSheet.showInView(self.view)
Run Code Online (Sandbox Code Playgroud)

创建Action表函数

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

 }
Run Code Online (Sandbox Code Playgroud)


Zor*_*ayr 14

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)
}
Run Code Online (Sandbox Code Playgroud)

这是它呈现的样子:

                                     Swift中的AlertViewController


Ian*_*nry 8

您永远不会设置操作表的委托:

myActionSheet = UIActionSheet()
myActionSheet.delegate = self
Run Code Online (Sandbox Code Playgroud)