简体   繁体   English

如何在Swift中将数据从UITableViewRowAction传递到UIViewController?

[英]How to pass data to a UIViewController from a UITableViewRowAction in Swift?

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. 我是iOS编程的新手,我一直在寻找执行该操作的方法,但似乎找不到我想要的东西。 I thought this would be a fairly easy task, and I'm sure it is if someone can enlighten me. 我认为这将是一个相当容易的任务,而且我敢肯定,这是否可以启发我。

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(...). 我要做的就是显示另一个视图控制器,但是将一些数据传递给该视图控制器,就像通常在prepareForSegue(...)中完成的方式一样。

Here is what I have. 这就是我所拥有的。 Please see the section marked "//Option 2" ... 请参阅标有“ //选项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)
}

How can I pass data to my view controller in the "editHandler" method? 如何通过“ editHandler”方法将数据传递给视图控制器? 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. 我可以使用segue整天显示视图控制器,但是我希望它与表中的选定项目一起加载,以便用户可以对其进行修改。

Thanks! 谢谢!

With Option1. 使用Option1。 You can pass data by sender. 您可以按发件人传递数据。

let selectedItem = self.items[indexPath.row] // I have an array of 'items'

performSegueWithIdentifier("EditItem", sender: selectedItem)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "EditItem" {
        let itemViewController = segue.destinationViewController as! ItemViewController
        if let item = sender as? Item {
            itemViewController.item = item
        }
    }
}

Either option 1 or option 2 is using the same technique to pass data, namely creating a new itemViewController instance and assign items to it. 选项1或选项2使用相同的技术来传递数据,即创建一个新的itemViewController实例并为其分配项目。

Your code seems fine with option 2, the problem is, what do you mean by 'does not work'? 您的代码在选项2中看起来还不错,问题是,“不起作用”是什么意思? You can add breakpoints at 您可以在以下位置添加断点

myViewController.item = selectedItem

and print out myViewController and item to see if thay are what you want. 并打印出myViewControlleritem以查看您想要的东西。 Then in myViewController 's viewDidLoad method, check if the item is still valid. 然后在myViewControllerviewDidLoad的方法,检查item仍然有效。

If everything is fine, you should be good. 如果一切都很好,那您应该很好。

I think, your problem is not 我想,你的问题不是

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

i'm sure it's correct. 我确定这是正确的。 So Do I 我也是
In your myViewController, you try print item in viewdidload to check it have data? 在您的myViewController中,您尝试在viewdidload中打印项目以检查其是否有数据?

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 因此,我结合发现以下问题,找出了问题所在:发现我原来想的还有一个不同的问题,并结合了本文中的信息: Swift presentViewController

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... 我必须在视图控制器Identity Inspector中分配一个“ Storyboard ID”(我将ID分配为“ EditItem”),然后我必须使用该ID实例化视图控制器并将其嵌入导航控制器中。

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM