Swift-alert动作处理程序协助?

时间:2016-06-04 12:00:21

标签: swift

我正在使用教程从自定义表格视图单元格添加电子邮件和iMessage“共享”操作。但我很困惑。在调用操作时,教程将停止为“print ln”。但不解释处理程序。有人可以帮忙吗?

我在()中添加了(ACTION:UIAlertAction!)作为占位符,作为一种猜测,但不确定在哪里继续使用。

谢谢。

 override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction] {

        let shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Share", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

            let shareMenu = UIAlertController(title: nil, message: "Send mail", preferredStyle: .ActionSheet)
            let emailAction = UIAlertAction(title: "Email", style:UIAlertActionStyle.Default, handler:  { (ACTION :UIAlertAction!)in})



            let imessageAction = UIAlertAction(title: "iMessage", style: UIAlertActionStyle.Default, handler:  {
                (ACTION :UIAlertAction!)in})

            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

            shareMenu.addAction(emailAction)
            shareMenu.addAction(imessageAction)
            shareMenu.addAction(cancelAction)



            self.presentViewController(shareMenu, animated:  true, completion: nil)
            }

                )
        shareAction.backgroundColor = UIColor(red: 109.0/255.0, green: 188.0/255.0, blue: 219.0/255.0, alpha: 1.0)


        return [shareAction]

2 个答案:

答案 0 :(得分:1)

首先,您应该创建UIAlertController,然后创建UIAlertActions并设置处理程序。然后创建UITableViewRowAction。你有点混淆了。这是带文档的代码。这几乎是不言自明的。

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

        // Construct alert controller first
        let shareController = UIAlertController(title: nil, message: "Send mail", preferredStyle: .ActionSheet)

        // Create email, iMessage and cancel actions
        let emailAlertAction = UIAlertAction(title: "Email", style: .Default) {
            action in
            let emailController = MFMailComposeViewController()
            if MFMailComposeViewController.canSendMail() {
                emailController.setSubject("This is subject")
                // Additional configuration
                self.showViewController(emailController, sender: self)
            }
        }

        let iMessageAlertAction = UIAlertAction(title: "iMessage", style: .Default) {
            action in
            print("iMessage action is selected")
        }

        let cancelAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) {
            action in
            // do nothing
        }

        // Add actions to alert controller
        shareController.addAction(emailAlertAction)
        shareController.addAction(iMessageAlertAction)
        shareController.addAction(cancelAlertAction)

        // Create shared action
        let shareAction = UITableViewRowAction(style: .Normal, title: "Share") {
            action, indexPath in
            // What happens when someone taps on Share
            print("selected share at cell index \(indexPath.row)")
            self.showViewController(shareController, sender: self)
        }
        shareAction.backgroundColor = UIColor(red: 109.0/255.0, green: 188.0/255.0, blue: 219.0/255.0, alpha: 1.0)

        return [shareAction]
    }

答案 1 :(得分:0)

我不确定你想要什么?如果你想处理一个动作?首先,您需要创建一个UIAlertController。并为其添加UIAlertAction

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: self.handler)) self.presentViewController(alertController, animated: true, completion: nil)

你的处理程序功能:

func handler(action:UIAlertAction) { // Do something }