Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
Right now I'm working on an iPhone app. There are several view controllers that are working with data that needs (for the most part) to be shared through all the controllers. Right now I have a DataController model class for this.

My AppDelegate class has an instance of the DataController class called myDataController. Each view controller also has an instance of a DataController. Whenever I init a new view controller, I set the new view controller's DataController equal to my old DataController (originally from the AppDelegate). So if View Controller A changes something, View Controller B (pointing to the same DataController) sees the change as well.

I'm just wondering if this is a good way to go about doing things. Would delegation be a better idea? Should I make my AppDelegate also function as a "Data Delegate" that can return the data that I need to each view controller?

Which approach is more efficient? Are both about the same? Is one more preferred?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Seems like you are trying to use DataController as a singleton object without correctly making it one. Follow the singleton pattern and all will be fine (this is what I do with my root data model object).

The pattern for this is covered in some detail in the Cocoa Fundamentals document provided by Apple that you should have read.
 

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
Seems like you are trying to use DataController as a singleton object without correctly making it one. Follow the singleton pattern and all will be fine (this is what I do with my root data model object).

The pattern for this is covered in some detail in the Cocoa Fundamentals document provided by Apple that you should have read.

Ahh. Yes, I should have thought of that.

So then all I would need is something like:

[ [DataController myOnlyDataController] myArray ];

To get myArray from anywhere in the app?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Yes, although it should be noted that, as per the example, the convention in Cocoa would be for the method to be called something like sharedDataController so the code would look like:

Code:
[[DataController sharedDataController] myArray]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.