0

I've been through some topics on SO, but I can't figure out how to use protocols properly for passing variables along to the parent view.

Basically, I've got a tableListView, which is a normal UIViewController, but with UITableViewDataSource and UITableViewDelegate attached to it so the view contains a table.

When pressing on an item in that table, the user moves along to detailView. I'd like to see how a specific value, say a String, can be passed from detailView to the tableListView (so I can mark a few cells, based on that String).

Anyone having a concrete example of doing this? You'd be the best <3

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • to make sure I undertand... UIViewController (vcA) holds a UITableView, upon pressing on a UiTableViewCell, you create another UIViewController (vcB) and push that view controller onto the UINavigationController's stack. You need to pass a string to vcB, and when the user manipulates, or changs that string, you need to update vcA to reflect the changes. Is that correct? – Jason Cragun Apr 28 '11 at 14:45
  • @Jason Cragun: That's pretty much correct, although vcA doesn't pass along that value to vcB, but vcB can still update the string in vcA. Both ways contain the same question for me: how can I update the value in vcA from vcB? – Joetjah Apr 29 '11 at 06:52

3 Answers3

0

You have to add a @property in the detailView and just before you push it, do:

[detailView setMyString:mystring];
albianto
  • 4,092
  • 2
  • 36
  • 54
0

Each View Controller should be managing a model. Your "List" view controller should be managing an array of model objects. When you populate the tableview, you should be using it's datasource methods to inspect the array of objects and display them. The index in the tableview and the index in the array will match up.

Your detail view should be managing a single object that is modeling the data. When your "list" tableview's delegate method fires, you create a new detail view, assign one of the objects from your array to the detail view property and push the detail view on the stack.

On the detail view you may have a "save" button, or just save the changes to the model object when the view is removed from the stack, either way you just update the reference to the model object that the detail view has. Once the list view is on top of the stack again you can execute the [self.tableView reloadData] on the list view. Any of the updates will now be visible.

Here is a link to Apples Sample Code http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007416

Mark Fruhling
  • 596
  • 6
  • 18
  • Thanks for the information! I'm afraid I'll need it the other way around, though :( – Joetjah Apr 29 '11 at 06:52
  • @Joetijah: Either way you need to follow the advice of manipulating your models and not worry about passing values between two controllers. The MVC pattern allows each controller to manipulate a model independent of other controllers. Your second view controller should manipulate your model and change the "string" value that is in your question. When you first view controller is visible again you need to update it's representation of the changed model – Mark Fruhling Apr 29 '11 at 15:03
0

It's all explained here.

Community
  • 1
  • 1
Joetjah
  • 6,292
  • 8
  • 55
  • 90