如何使用swift激活操作表按钮? [英] How to activate action sheet buttons with swift?

查看:93
本文介绍了如何使用swift激活操作表按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来链接用户的功能表按钮

I am looking for a way to link the buttons of the action sheet functional for the user

我使删除按钮功能正常但我需要报告按钮制作用户能够发送电子邮件,如何用swift做到这一点?谢谢

i made the Delete button functional but i need for the Report button making the user be able to send E-mail, how to do that with swift? Thank you

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

    var ReportAction = UITableViewRowAction(style: .Default, title: "Report User", handler: {(action: UITableViewRowAction! , indexPath:NSIndexPath!) -> Void in

        let ReportMenu = UIAlertController(title: nil, message: "Report using", preferredStyle: UIAlertControllerStyle.ActionSheet)
        let Reportbutton = UIAlertAction(title: "E-Mail", style: .Default , handler: nil)

        ReportMenu.addAction(Reportbutton)
        self.presentViewController(ReportMenu, animated: true, completion: nil)


    })

        var DeleteButton = UITableViewRowAction(style: UITableViewRowActionStyle.Default , title: "Delete", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

            self.Array1.removeAtIndex(indexPath.row)
            self.Array2.removeAtIndex(indexPath.row)
            self.Array3.removeAtIndex(indexPath.row)
            self.Array4.removeAtIndex(indexPath.row)
            self.Array5.removeAtIndex(indexPath.row)


            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

    }
    )
    return [DeleteButton , ReportAction]



}


推荐答案

你可以这样做:

let Reportbutton = UIAlertAction(title: "E-Mail", style: UIAlertActionStyle.Default) { (alert) -> Void in

            //Your code
        }

这是您发送电子邮件的示例代码:

Here is the example code for you to send E-mail:

import UIKit
import MessageUI

class TableViewController: UITableViewController, MFMailComposeViewControllerDelegate {

var tabledata = ["1","2","3","4"]
override func viewDidLoad() {
    super.viewDidLoad()
}
// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return tabledata.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    // Configure the cell...
    cell.textLabel?.text = tabledata[indexPath.row]
    return cell
}

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

    var ReportAction = UITableViewRowAction(style: .Default, title: "Report User", handler: {(action: UITableViewRowAction! , indexPath:NSIndexPath!) -> Void in

        let ReportMenu = UIAlertController(title: nil, message: "Report using", preferredStyle: UIAlertControllerStyle.ActionSheet)
        let Reportbutton = UIAlertAction(title: "E-Mail", style: UIAlertActionStyle.Default) { (alert) -> Void in

            self.displayMailComposerSheet()
        }
        ReportMenu.addAction(Reportbutton)
        self.presentViewController(ReportMenu, animated: true, completion: nil)


    })

    var DeleteButton = UITableViewRowAction(style: UITableViewRowActionStyle.Default , title: "Delete", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        self.tabledata.removeAtIndex(indexPath.row)



        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

        }
    )
    return [DeleteButton , ReportAction]
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    switch editingStyle {
    case .Delete:

        // remove the deleted item from the model
        self.tabledata.removeAtIndex(indexPath.row)
        // remove the deleted item from the `UITableView`
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    default:
        return
    }
}

func displayMailComposerSheet(){

    //vars
    var controller = MFMailComposeViewController()
    controller.mailComposeDelegate = self

    //set
    controller.navigationBar.tintColor = UIColor.whiteColor()

    //set
    UINavigationBar.appearance().translucent = false
    UINavigationBar.appearance().barTintColor = UIColor.whiteColor()
    UINavigationBar.appearance().tintColor = UIColor.blackColor()
    var attributes = NSDictionary(objectsAndKeys: UIFont(name: "HelveticaNeue-Light", size: 20)!,NSFontAttributeName, UIColor.whiteColor(), NSForegroundColorAttributeName)
    UINavigationBar.appearance().titleTextAttributes = attributes

    //set
    var toRecipients = NSArray(object: "support@YourDomain.com")
    controller.setToRecipients(toRecipients)
    controller.setSubject("App Support")

    //show
    presentViewController(controller, animated: true, completion: {

        UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
    })
    }
}

这篇关于如何使用swift激活操作表按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆