I have application with following design:
Table View that lists documents.
And ViewController where document is edited.
When editing the document I want to use a copy of object so in case user press Cancel I just throw away the copy. If user selected Save I copy modified document to original.
(I can’t have all document’s fields as properties in viewController because there are too many of them).
Table View controller:
SelectedRowAtIndex {
document= [documentsArray objectAtIndex:indexPath.row];
viewController.assignedDocuemnt= document;
}
Then I have View controller class where the item is modified:
@interface
@property (nonatomic,retain) Document *asignedDocument;
@property (nonatomic,retain) Document *editedDocument;
viewDidLoad{
editedDocument= [assignedDocuemnt copy];
}
IBAction save {
assignedDocument=editedDocument;
}
My problem is that original document in documentsArray is not updated with edited version.
What did I miss?
You’re copying the value and then assigning the reference of the copy back over a reference to the original. That’s definitely not going to work.
First, keep track of which document they’re adding in the table view’s viewcontroller.
Then add a handler for replacing the selected document with another to the table view’s viewcontroller:
To save, call the table view controller’s new message from the editor view controller with something like:
A better way to do this would be to modify your editor view controller to take a target and action instead of calling back to the table view controller explicitly. When you want to save, call the action:
But that’s another bump on the learning curve that you probably don’t need to get over today.