Preferred method of sharing data between UIViewControllers

42 views
Skip to first unread message

bwalrond

unread,
Aug 10, 2009, 5:02:35 PM8/10/09
to iPhone Application Development Auditors
Hello:

I have a question about what the preferred method of sharing data
between UIViewControllers is in the spirit of the MVC model.

For example, suppose the user selects the 'add' button from the nav
bar - a new ViewController is pushed on the stack - user enters new
data on a form then presses the 'done' button. Should the top
viewcontroller be responsible for persisting the new data or pass the
data back to the rootviewcontroller to be responsible for persisting
the data. And if the later, then how should the top viewcontroller
pass the data back before it get's 'pop'ed and disappears?

Any thoughts or examples appreciated!

(Apologies in advance if this question has already been posed and
answered.)

Thanks for any help you can offer.

Brian Gibson

unread,
Aug 11, 2009, 12:45:35 AM8/11/09
to iphone-appd...@googlegroups.com
If the data is encapsulated in a model instance, and both controllers have a reference to that instance, then just set the model data in one controller and read it in the other.

That's one way to do it.

loren...@gmail.com

unread,
Aug 11, 2009, 10:07:40 AM8/11/09
to iPhone Application Development Auditors
One can use nsuserdefaults as well. I use It for simple data types
i.e.: an nsstring is set in one vc stored in user defaults and read
back in another vc.

Sadat

unread,
Aug 11, 2009, 10:56:11 AM8/11/09
to iPhone Application Development Auditors
Using NSUserDefaults to share states between two view controllers is
no better than using a global variable or a singleton, thus
introducing coupling / complicating testability, and in essence going
against the principle of inversion of control containers (aka
dependency injection). Stick to injecting a model instance and you
could have a method - (void)persistModelData within the model which
can write out to NSUserDefaults or SQLite or any other suitable
persistence medium. - Just my 2c.

On Aug 12, 12:07 am, "lorenzo7...@gmail.com" <lorenzo7...@gmail.com>
wrote:

Sanjeev Rana

unread,
Aug 11, 2009, 10:56:51 AM8/11/09
to iphone-appd...@googlegroups.com
Hi, 
I think you can set up a delegate for your top view controller and pass the what ever data you want to pass. The it all depends on you how do u want to handle the call back in delegate. You can store in NSuserdefault or your model object.

2009/8/10 bwalrond <bwal...@gmail.com>

bwalrond

unread,
Aug 12, 2009, 7:53:17 AM8/12/09
to iPhone Application Development Auditors
Thanks to all for your responses!

Brian ... good point.

Sanjeev ... your notion of setting up a delegate seems interesting.
Do you know of an example that comes with the SDK that demonstrates
this method?

Thanks again!

Bill

On Aug 11, 10:56 am, Sanjeev Rana <ransanj...@gmail.com> wrote:
> Hi,
> I think you can set up a delegate for your top view controller and pass the
> what ever data you want to pass. The it all depends on you how do u want to
> handle the call back in delegate. You can store in NSuserdefault or your
> model object.
>
> 2009/8/10 bwalrond <bwalr...@gmail.com>

Sanjeev Rana

unread,
Aug 12, 2009, 8:55:11 PM8/12/09
to iphone-appd...@googlegroups.com
Not sure of any example as of now. But you can do this.

Just declare a protocol for your detail view controller. Then declare a delegate class which implements this protocol  and when you create your detail view, at that time you can create your delegate object and set this as a delegate for  detail view controller. You can also use your list view controller as a delegate or you declare a new delegate class for this.

Hope this helps.

2009/8/12 bwalrond <bwal...@gmail.com>

bwalrond

unread,
Aug 13, 2009, 8:37:52 AM8/13/09
to iPhone Application Development Auditors
Awesome Sanjeev. I'm going to give that a try!

Thanks for all your help!

Bill

On Aug 12, 8:55 pm, Sanjeev Rana <ransanj...@gmail.com> wrote:
> Not sure of any example as of now. But you can do this.
>
> Just declare a protocol for your detail view controller. Then declare a
> delegate class which implements this protocol  and when you create your
> detail view, at that time you can create your delegate object and set this
> as a delegate for  detail view controller. You can also use your list view
> controller as a delegate or you declare a new delegate class for this.
>
> Hope this helps.
>
> 2009/8/12 bwalrond <bwalr...@gmail.com>

nick fox

unread,
Aug 13, 2009, 2:46:42 PM8/13/09
to iPhone Application Development Auditors
The injection method seems to be the preferred method. Read this
answer on stackoverflow:

http://stackoverflow.com/questions/569940/whats-the-best-way-to-communicate-between-viewcontrollers/575497#575497

Nick

bwalrond

unread,
Aug 22, 2009, 5:07:24 PM8/22/09
to iPhone Application Development Auditors
Interesting. Thanks, Nick.

On Aug 13, 2:46 pm, nick fox <nicolas...@gmail.com> wrote:
> The injection method seems to be thepreferredmethod. Read this
> answer on stackoverflow:
>
> http://stackoverflow.com/questions/569940/whats-the-best-way-to-commu...
>
> Nick

marcio

unread,
Aug 22, 2009, 6:47:02 PM8/22/09
to iPhone Application Development Auditors
I think the model object is my first choice. And it's implementation
would be in the delegate.

I'd like to suggest to the moderators if possible to use sticky posts
to address certain core topics, is it possible? Does google groups
have that?

bwalrond

unread,
Aug 25, 2009, 2:26:51 PM8/25/09
to iPhone Application Development Auditors
Another reasonable example of sharing data between UIViewControllers
is from Dave Mark and Jeff LeMarche in their book Beginning iPhone 3
Development.

www.iphonedevbook.com

Check out the Nav project and the editable detail pane (the
presidents) example - Chapter 9.

marcio

unread,
Aug 25, 2009, 5:20:23 PM8/25/09
to iPhone Application Development Auditors
im sure the book is great, but i think (and i mean this w/no
disrespect to you :)) so pls dont be offended, that we are by nature a
digital-now kinda people, especially since we are coders for a device
that is oriented towards that very fact. so this would involve having
to go buy a book! :) which takes time we might not have right now :)

but thanks for the tip

bwalrond

unread,
Aug 25, 2009, 6:55:51 PM8/25/09
to iPhone Application Development Auditors
Well then ... here you go Mr Digital-Now:

http://books.google.com/books?id=WV9glgdrrrUC&pg=PT27&dq=beginning+iphone+dev#v=onepage&q=&f=false

How's that grab ya?

marcio

unread,
Aug 25, 2009, 8:48:57 PM8/25/09
to iPhone Application Development Auditors
thx! gr8 idea :)

On Aug 25, 3:55 pm, bwalrond <bwalr...@gmail.com> wrote:
> Well then ... here you go Mr Digital-Now:
>
> http://books.google.com/books?id=WV9glgdrrrUC&pg=PT27&dq=beginning+ip...
> > > > > > Nick- Hide quoted text -
>
> - Show quoted text -

Duane Bender

unread,
Nov 10, 2012, 3:28:42 PM11/10/12
to iphone-appd...@googlegroups.com
It has been a long time since I did Assignment 6, but I'll try to help. First, yes I think it is okay for two VCs to share a single model. But be sure you have a mechanism for ensuring that each VC is notified of any changes the other VC makes to the model, perhaps using the observer/notification pattern. But in this assignment, I don't think the two VCs share a model. As I remember, the model for the Calc-VC was a CalculatorBrain, and the model for the graph-VC was simply a "program" (an NSArray holding a stack of operands and operations). Hope that helps.

Duane

On Friday, November 9, 2012 8:35:08 AM UTC-6, PeteOsler wrote:
I'm slogging my way through lecture 6 of fall 2011, he's talking about multiple view controllers and segues.  He mentions that the seque-ed to view controller (call it toVC) should never contain a back-pointer to fromVC.  But what about fromVC and toVC sharing models--is that legit?  In the calculator example, does the graph-VC have to have a different model than the calc-VC?  The uber-question is:  in the MVC paradigm, does every MVC each have to have an M, a V, and a C, or can there be a single M and multiple Vs and Cs?  (To me, seems like the latter is the way to go, but I'm worried about some subtlety that I dont remember that was introduced in lecture 1)
Reply all
Reply to author
Forward
0 new messages