Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-15-2010, 08:39 AM   #1 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default Passing data/confirmation between views

Hey guys,

What is the "right" way (from a good software architect point of view) to pass data between view controllers. Here is an example:

I have a UINavigationBar with HomeViewController as the root. From the HomeViewController, it can lead to two other view controllers CartViewController and AddItemViewController. The user has to do some stuff in the AddItemViewController (get product info, confirmation, etc...) and then *data* is passed and displayed to the CartViewController to be displayed in CartView.

My question is, should the AddItemViewController pass the data directly to CartViewController or should be pass the data back to the root view controller (ie: let the root view controller handle/manage all the data) and then pass it to the other view controllers.

I hope my question makes some sense.

Thanks!
Yko is offline   Reply With Quote
Old 09-15-2010, 12:43 PM   #2 (permalink)
N3RD
 
Join Date: Oct 2009
Posts: 201
tychop is on a distinguished road
Default

You could use CoreData.
Or you could add some sort of service singleton object in the appdelegate and put DTO's in there. From a VC you van easily get to the appdelegate
tychop is offline   Reply With Quote
Old 09-15-2010, 01:40 PM   #3 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default

Quote:
Originally Posted by tychop View Post
You could use CoreData.
Or you could add some sort of service singleton object in the appdelegate and put DTO's in there. From a VC you van easily get to the appdelegate
Sorry for the newbness. I can read up on CoreData and have used singleton before but what is DTO and VC?

Thanks.
Yko is offline   Reply With Quote
Old 09-15-2010, 02:07 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: Stockholm, Sweden
Posts: 38
Jompe71 is on a distinguished road
Default

Quote:
Originally Posted by Yko View Post
Sorry for the newbness. I can read up on CoreData and have used singleton before but what is DTO and VC?

Thanks.
DTO, Data Transfer Object.

I've had similar thoughts as you have and came to the conclusion in my apps to do like a reversed MVC.
By this I mean that I use a singleton to handle all data loading and "storage", pretty much like tychop states.

So every time a view needs data it queries the singleton for it and that singleton is responsible for loading missing data or supplying data (like from a shoppingcart) whenever requested.
So this patterns is opposite from "broadcasting" data to N views where you in your controller do not know which view will need what data.

A metaphor would be like in a web application you load data from the backend to fulfill the need of page <alphabet>.jsp when the browser only requests a.jsp.

Bottom line is that if a view adds data to the storage/singleton class like the user adds more items the the shopping cart you do not pass that data to the check out view. The check out view asks for the data from the same singleton class when it is shown.

Hope I explain my thoughts well enough...
Jompe71 is offline   Reply With Quote
Old 09-15-2010, 02:40 PM   #5 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default

Quote:
Originally Posted by Jompe71 View Post
DTO, Data Transfer Object.

I've had similar thoughts as you have and came to the conclusion in my apps to do like a reversed MVC.
By this I mean that I use a singleton to handle all data loading and "storage", pretty much like tychop states.

So every time a view needs data it queries the singleton for it and that singleton is responsible for loading missing data or supplying data (like from a shoppingcart) whenever requested.
So this patterns is opposite from "broadcasting" data to N views where you in your controller do not know which view will need what data.

A metaphor would be like in a web application you load data from the backend to fulfill the need of page <alphabet>.jsp when the browser only requests a.jsp.

Bottom line is that if a view adds data to the storage/singleton class like the user adds more items the the shopping cart you do not pass that data to the check out view. The check out view asks for the data from the same singleton class when it is shown.

Hope I explain my thoughts well enough...
Your explanation is very well. Thank you very much and I guess that is what the MVC model is all about. At first, I read in a book that usually the root view controller handles all the data and passes data to the view controllers that needed it but I may have misinterpret that. Now, I think the root view controller is responsible for handling all the controllers and there is a data model which as you mentioned is the singleton class. Then, the root view controller's responsibility is to mainly refer that data model to the different view controllers and each view controller interacts directly to the data model.

It might not be a perfect view at the moment but I think it is better than what I had before. Thanks again

Yko
Yko is offline   Reply With Quote
Old 09-15-2010, 02:44 PM   #6 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default

My other question from this discussion is that I though Singleton is bad. When I first try to pass data between different views, I was told to google singleton and I did but was told that it broke the MVC when you let everything be global...
Yko is offline   Reply With Quote
Old 09-16-2010, 09:05 AM   #7 (permalink)
N3RD
 
Join Date: Oct 2009
Posts: 201
tychop is on a distinguished road
Default

Quote:
Originally Posted by Yko View Post
My other question from this discussion is that I though Singleton is bad. When I first try to pass data between different views, I was told to google singleton and I did but was told that it broke the MVC when you let everything be global...
I think it's not always bad practice. I use it to create a service class which act like a broker to my data. The service & VC (View Controller) should not know how to r/w data. That's the job of the persistence layer which I call from my service class. The persistence layer then can be swapped out easily if you decide to use a different kind of storage. (Separation of concerns).
Since I only need 1 service object, I think a singleton is just fine.
tychop is offline   Reply With Quote
Reply

Bookmarks

Tags
data, uinavigationbar, view controller, views

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 325
19 members and 306 guests
2ndSegment, bignoggins, cayladv57, cgokey, dermotos, djohnson, Domele, guusleijsten, Hamad, heshiming, linkmx, markuschow, Objective Zero, pungs, Rudy, Sloshmonster, teebee74, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:53 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0