How to pass data to UIViewController from UITableViewRowAction to Swift?

I am new to iOS programming and I was looking for how to do this, but I can't seem to find what I'm looking for. I thought it would be a fairly simple task, and I am sure that if someone can enlighten me.

When scrolling left on the table view, I have some user actions that can be performed. One such action is to simply “Edit” the selected item. All I want to do is display another view controller, but pass some data to this view controller, as is usually done in prepareForSegue (...).

Here is what I have. See Section "// 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, , , .

!

0
4

Option1. .

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
        }
    }
}
0

1 2 , itemViewController .

2, , " "?

myViewController.item = selectedItem

myViewController item, , . myViewController viewDidLoad , item.

, .

0

, ,

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

, . myViewController viewdidload, ?

0

, , , , , : Swift presentViewController

, . " " ( "EditItem" ), ...

    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)
0

Source: https://habr.com/ru/post/1651751/


All Articles