Reputation: 15
I'm using a tutorial to add an email and iMessage "share" action from a custom table view cell. But I am confused. The tutorial stops as "print ln" when invoking the action. But doesn't explain the handler. Can anyone help?
I added the (ACTION :UIAlertAction!)in}) as a placeholder, as a guess, but not sure where to go with the rest.
Thank you.
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]
Upvotes: 0
Views: 939
Reputation: 4533
First, you should create UIAlertController
, then create UIAlertActions
and setup handlers. Then create UITableViewRowAction
. You got it somewhat mixed up. Here is the code with documentation. It's pretty much self-explanatory.
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]
}
Upvotes: 1
Reputation: 156
I'm not sure what you want? If you want to handle an action? First of all you'll need to create an UIAlertController
. And add an UIAlertAction
to it.
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)
And your handler function:
func handler(action:UIAlertAction) {
// Do something
}
Upvotes: 0