views:

54

answers:

1

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?

+1  A: 

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:

- (void)replaceSelectedDocument:(Document*)newDoc {
    [documents replaceObjectAtIndex: editingIndex
                         withObject: newDoc];
}

To save, call the table view controller's new message from the editor view controller with something like:

[tableViewController replaceSelectedDocument: editedDocument];

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:

[target performSelector: action withObject: editedDocument];

But that's another bump on the learning curve that you probably don't need to get over today.

Steven Fisher
Thanks for the answer.Could you elaborate about last part, the "better way" ? I'm holding the documents array in separate Data class as singleton.
Rod
I just meant better compared to hard-coding which message to send to the table view controller. If you send `replaceSelectedDocument`, you've forever coupled those objects. If you instead pass `target: self action: @selector(replaceSelectedDocument:)`, you've kept it flexible and uncoupled: The editor doesn't actually care that it was called by the table view controller, it just knows it should send a message specified to it to a target specified to it when it's done.
Steven Fisher