使用Swift编写UIActionSheet

10

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

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

/// UIActionSheetDelegate

/// 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)

有什么解决问题的想法吗?


4
动作表(action sheet)和警告框(alert view)在iOS 8中已经过时(deprecated),并引入了alertController。 - nikhil84
3
是的,虽然它已经被弃用了,但如果您要支持iOS 7上的应用程序,那么alertController将无法使用。因此最好检查iOS版本,并针对iOS 8和iOS 7调用适当的代码。 - Mahmud Ahsan
请查看此答案 - http://stackoverflow.com/questions/27787777/how-to-create-uiactionsheet-actions/27798750#27798750 - Kampai
5个回答

20

使用 Swift 语言的 UIActionSheet:

带有取消按钮和破坏性按钮的操作表

设置 UIActionSheetDelegate。

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

带有取消按钮、破坏性按钮和其他按钮的操作表

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

 }

我写了一个Swift版本,不依赖于硬编码的按钮索引,参考另一个答案:https://dev59.com/JFHTa4cB1Zd3GeqPP0T3#29272140 - stone

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

以下是呈现的样子:

                                    Swift中的AlertViewController


8

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

myActionSheet = UIActionSheet()
myActionSheet.delegate = self

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 advanceAction = 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(advanceAction)
    optionMenu.addAction(cancelAction)
    
    // 5
    self.present(optionMenu, animated: true, completion: nil)
}

方法调用:

@IBAction func actionOnPaymentModeButton(_ sender: Any) {
    // open action sheet
    showPaymentModeActionSheet()
}

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

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接