tags:

views:

72

answers:

1

I've got a table view controller. Some of the rows of the table open new controllers to let the user enter more data or use pickers, etc. It's just like the built-in Calendar app. When the user taps "Save" on the second screen, I want the value from that screen to propagate back up into the table view controller, but I don't know how to do it. Since I'm using pushNavigationController to get to the second screen, there's no opportunity to provide a pointer back into the original object. (That seems like really bad design even if it were possible since the second controller would have to know stuff about its parent.)

Also, from testing, it seems like pushNavigationController doesn't 'pause' the original controller so you don't just resume on the next line of code once the second screen is popped.

Seems like I'm missing something really basic here.

+1  A: 

Many of Apple's frameworks support the MVC (Model View Controller) design pattern. The controller (UITableViewController in this case) orchestrates acquisition of the data for handoff to the view (a UITableView in this case). To take full advantage of this pattern your data should come from a Model object. This is an object you design to represent your data - providing an abstraction so that your UI implementation and your data are loosely coupled. When your controller pushes another controller to further inspect or edit the data the Model object is passed to this controller so the new controller can access the data contained in the model. The user changes the data in the UI and then when "Save" is pressed the model object is modified.

There are a number of ways to propagate the change back to the UITableViewController when the user presses the "Save" button. You can use KVO (key value observing) whereby the table or one of its cells is notified when a property of the model object changes. Another way is to use NSNotificationCenter notifications between the two controllers to communicate the change.

m4rkk
I ended up using the target-action pattern since I found a bunch of references to it in the Cocoa docs. I pass the view controller and a selector to a method within it to the sub-controller, which calls it when the button press is detected. So the sub-controller does a [target performSelector:action withObject:whateverTheDataWasThatChanged];This way the sub controller doesn't really know anything about what called it.I feel like a monkey with a shotgun right now. So far nobody has been killed though. :)
jsd