views:

18

answers:

3

Hello guys,

I have a UITableViewController and in the didSelectRowAtIndexPath method I create an instance of a UIViewController and push it on to the stack.

The UIViewController is meant to edit the content of the selected cell but how do I get the changes made in the ViewController transfered back to the TableViewController?

Cheers

A: 

Override the "parent" or table view controller's -viewWillAppear: method and reload the table view data there, using [tableView reloadData];.

When you go back to the table view controller from the edit view controller, the table view controller's -viewWillAppear: method reloads the data, which in turn calls the table view delegate methods.

Alex Reynolds
Yeah but how do i link the data?In the tableviewcontroller didSelectRowAtIndexPath i create an instance of the uiviewcontroller then accesses the properties and then push the uiviewcontroller onto the stack. The uiviewcontroller now has the data from the tableviewcontroller but how do we go the other way?
objneodude
How this is done depends on how you set up your table view controller. But you could keep the model as a table view property that your table view and edit view controllers can access and manipulate. Apple's Core Data tutorial covers one approach that uses Core Data, but the idea is the same: http://developer.apple.com/iphone/library/documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html
Alex Reynolds
A: 

You have to make sure that the two controllers 'share' data somehow. The easiest option is to have the first controller pass in the array (or just the selected object) that is selected in the table. The second view would then directly modify that array (or single object) and then when you dismiss the second view will show the modified data.

You might have to reload the table.

Makes sense?

willcodejavaforfood
Yeah but it is how i make them share data that i cannot wrap my head around atm...
objneodude
You do not know how to declare properties in a class and how to set them?
willcodejavaforfood
A: 

The standard way to pass data back up the hierarchy is with delegation or even more simply through querying a property on the edit view controller when you want to get the edited data back.

Now, when it comes to edit view controllers you can design them to support both direct editing of 'live' objects, or a copy which will let you do a Save/Cancel model.

So what you want is for your edit view controller to edit some sort of ModelObject instance with various properties. These properties will correspond to the textfields or date-pickers etc. in the view. So you might have a Person with NSDate *dateOfBirth and NSString *name.

When you create the view controller and push it onto the navigation controller, you give it one of these ModelObjects to edit. You can either pass in an object straight from your model which will be edited 'live' as the user enters values, or a copy which will let you discard changes and implement a Save or Cancel workflow. For the latter you can add the Save and Cancel buttons yourself before you push the edit view which lets you handle the actions yourself without needing delegation.

So your edit view controller will set the properties on this object when the user enters a new value in a textfield or changes the date picker. For a live object these changes will be applied immediately to your model. For a copy, when the user hits Save you query the edit view controller for the object you passed in and merge/copy that back into your model. If the user hits cancel, you just discard the object.

Mike Weller