tags:

views:

29

answers:

1

Hello. I'm diving into iOS development and am getting familiar with navigation view controllers. I'm trying to build a simple app with a table view that allows me to add objects to it. So far, I have a table view with an add "+" button in the nav bar that allows me to load my CreateObjectView and display it modally so the user can define the new object, but when they click the save button, I don't know how to send that object data back to the parent view that created the CreateObjectView that contains the object data.

When I create the child view (CreateObjectView), I could pass into it a pointer to the current view before I push it onto the nav stack, but this just feels dirty and circular.

I was reading about protocols and delegates and I could use that approach as well, but it also feels circular.

This question seems like it would be a common design issue to any tableview-based or nav-based app, how should I probably access the view that is one level up from my current view in a nav-based iOS app?

Thanks so much in advance for all your help!

+1  A: 

It feels circular at first, but it is the right way. Before the UINavigationController pushes the new view controller you should set the delegate to the current view controller - assuming that's the object you wish to communicate with. Of course you could set it somewhere else instead, such as to a central Core Data handler. Then when you need to communicate call the delegate with the method you have defined. I've done several navigation apps like this after seeing Apple's cookbook example, and it really works well.

Adam Eberbach
thanks, adam! the delegate approach seems to work well, now i'm going to try and refactor my code and create a central core data handler. can you give me a link to Apple's cookbook example? I can't seem to find it.
BeachRunnerJoe
I cannot find a link to it now but it is the example used in the 2009 WWDC video session regarding structure of iPhone applications.
Adam Eberbach