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.
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(...).
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)
}
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.
Thanks!