如何在Swift中将数据从UITableViewRowAction传递到UIViewController? [英] How to pass data to a UIViewController from a UITableViewRowAction in Swift?
问题描述
我是iOS编程的新手,我一直在寻找实现方法,但似乎找不到我想要的东西.我认为这将是一个相当容易的任务,而且我敢肯定,这是否可以启发我.
I'm new to iOS programming, and I've been searching for how to do this, but can't seem to find what I'm looking for. I thought this would be a fairly easy task, and I'm sure it is if someone can enlighten me.
当您在表格视图上向左滑动时,我可以执行一些自定义操作.这些动作之一是对所选项目的简单编辑".我要做的就是显示另一个视图控制器,但是将一些数据传递给该视图控制器,就像通常在prepareForSegue(...)中完成的方式一样.
When you swipe left on a table view I have some custom actions that can be performed. One of those actions is a simple "Edit" of the item selected. All I want to do is display another view controller but pass some data to that view controller like how it's normally done in prepareForSegue(...).
这就是我所拥有的.请参阅标有"//选项2"的部分...
Here is what I have. Please see the section marked "//Option 2" ...
// Override to provide additional edit actions when the users swipes the row to the left.
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(
style: UITableViewRowActionStyle.Normal, title: "Delete", handler: deleteHandler);
deleteAction.backgroundColor = UIColor.redColor()
let editAction = UITableViewRowAction(
style: UITableViewRowActionStyle.Normal, title: "Edit", handler: editHandler);
editAction.backgroundColor = UIColor.grayColor()
return [deleteAction, editAction]
}
// Handles the delete action when the users swipes the row to the left.
func editHandler(action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void {
// Option 1
//performSegueWithIdentifier("EditItem", sender: nil)
//Option 2 - This does not work.
let myViewController = ItemViewController()
let selectedItem = self.items[indexPath.row] // I have an array of 'items'
myViewController.item = selectedItem
self.presentViewController(myViewController, animated: true, completion: nil)
}
// Handles the delete action when the users swipes the row to the left.
func deleteHandler(action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void {
self.games.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
如何通过"editHandler"方法将数据传递给视图控制器?我可以使用segue整天显示视图控制器,但我希望它与表中的选定项目一起加载,以便用户可以对其进行修改.
How can I pass data to my view controller in the "editHandler" method? I can display the view controller all day long using the segue, but I want it to be loaded with the selected item in the table so that it can be modified by the user.
谢谢!
推荐答案
因此,我结合以下发现找出了一个问题:发现我遇到的问题与我最初的想法不同,并且结合了本文中的信息: 迅速呈现presentViewController
So I figured out the problem from a combination of discovering that I had a different issue going on then what I originally thought, and with the information in this post: Swift presentViewController
我的视图控制器已嵌入导航控件中,创建视图控制器时需要进行一些额外的设置.我必须在视图控制器Identity Inspector中分配一个"Storyboard ID"(我将ID分配为"EditItem"),然后必须使用该ID实例化视图控制器并将其嵌入到导航控制器中.>
My view controller was embedded in a navigation control, and this requires some extra setup when creating the view controller. I had to assign a "Storyboard ID" in the view controllers Identity Inspector (I assigned the id of "EditItem"), and then I had to instantiate the view controller with that ID and embed it in a navigation controller...
let itemViewController = self.storyboard?.instantiateViewControllerWithIdentifier("EditItem") as! ItemViewController
itemViewController.item = self.items[indexPath.row]
let navigationController = UINavigationController(rootViewController: itemViewController)
self.presentViewController(navigationController, animated: true, completion: nil)
这篇关于如何在Swift中将数据从UITableViewRowAction传递到UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!