views:

69

answers:

2

I've got a view controller, call it VC1, that's a table view. When I tap a cell in the table view, I am presented with a new view controller, call it VC2, which is a short list of choices. After making a choice, I want to dismiss VC2 and set the cell.textLabel.text property of the VC1 cell I originally tapped to the value I selected in VC2.

Conceptually speaking, what is the proper way to do this? I've tried a handful of different approaches, but all of them seem wonky at best, and only one of them actually worked - although it was the most cumbersome of all, passing references to both view controllers and table view cells and all kinds of things. It just feels like I'm making a mountain out of what is probably a mole hill.

This is such a common paradigm that I find it hard to believe there's not a simple method for doing it. Thanks in advance for any input you can offer.

+1  A: 

There are a number of ways to handle this but one of the most flexible is via the Delegate Pattern. Define a delegate protocol in VC2's interface and then make VC1 conform to that protocol. When you create VC2 assign VC1 as it's delegate (similar to the way you did with your UITableView). One requirement of your protocol should be a method like didFinishWithStringSelection: (or whatever you call it) where you would update the table cell and reloadTable.

Meltemi
A: 

I do kind of that with with a table view presenting several attributes of an data object. To change a single attribute the user has to select the specific table cell showing the attribute he wants to change and that will push a new view controller (with a picker in my case) where the user can change the value from a selection.

I assign the data object to a property of the new controller before pushing. With this the value can be changed directly in the data object, and when i return (via navigation controller) to the first view controller there is a reloadData in viewWillAppear.

Did you try it this way?

Frank Martin
the 'reloadData' part is what I was missing; when I added that, the world was right again.Thanks!
Andy