Reputation: 2129
Is there a way to pass Core Data objects from DetailViewController to another View Controller to allow Editing?
From MasterViewController navigate to DetailViewController:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let myValue = myValues[indexPath.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = myValue
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
Then From DetailViewController to EditViewController:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showEdit" {
let controller = editViewController
controller.editItem = detailItem
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
But i get nil on my object
EDIT
The edit Item is set like this on EditViewController:
var editItem: MyValues? {
didSet {
// Update the view.
self.configureView()
}
}
and the detailItem is set like this in DetailViewController:
var detailItem: MyValues? {
didSet {
// Update the view.
self.configureView()
}
}
Upvotes: 2
Views: 139
Reputation: 31016
Instead of using a property for the edit controller, use what you get from the segue.
I.e. (Assuming you're moving directly to your EditViewController): Replace...
let controller = editViewController
...with something like...
let controller = segue.destinationViewController as! EditViewController
...except you should probably test it rather than using as!
.
===
Updating with my guess about how this came to be. (See comments.)
You must have created whatever editViewController
points to in code someplace, which means it's not the same instance/object that is created for you as part of the segue mechanism. In other words, you had two different EditViewController
objects and you were not passing data to the one that was going to be putting views on the screen.
Upvotes: 2