Re: Newbie Object Sharing Question
Re: Newbie Object Sharing Question
- Subject: Re: Newbie Object Sharing Question
- From: Joseph Crawford <email@hidden>
- Date: Fri, 30 Jan 2009 14:51:40 -0500
Why don't you create a base controller that those 2 controllers
subclass, that way they would inherit the functionality of reading
those properties, also they would inherit the methods for getting,
setting, etc.
BaseController
-- Controller 1
-- Controller 2
in Base controller define those properties and any methods for
interacting with them
in Controller 1 and Controller 2 have your Class specific stuff there,
because they both inherit from BaseController you would get those
properties.
Joseph Crawford
On Jan 30, 2009, at 2:46 PM, Brian Slick wrote:
I'm sure there is a really easy concept somewhere that I'm just not
getting, but well I'm just not getting it. This is for the iPhone,
but I believe it is generic to Cocoa.
Let's say I'm making a task list program, and the primary UI is a
UITabBar. On one tab, I want a table view presenting the list a
certain way, and on another tab I want a table view presenting the
same list in a different way. Let's assume the user can create new
items in either view.
I envision a couple of model objects like so:
MyListItem
MyListItemArray
And a couple of view controllers:
ViewControllerTabA
ViewControllerTabB
I want both view controllers to reference MyListItemArray. So in
each one I do something like:
MyListItemArray *listArray = [[MyListItemArray alloc] init];
I then provide the various table delegate methods, and link those to
listArray. I fire up the program, and see a few items in my list.
I add a couple more items, then tap on the other view. The new
items are not there. Using the log I have verified that the array
size is the same as it started, so the new items simply are not there.
I throw in a few more log comments, trying to figure out exactly
what I have. I see the log messages for the initialization of the
list items, but then I notice when I tap the next tab, I see those
same initialization messages again. It starts to sink in that what
I have done is create a *local* instance of MyListItemArray in each
view controller, and what's local to ViewControllerTabA has nothing
to do with what is local to ViewControllerTabB. I'm getting local
copies of the array, not the source array.
It starts to occur to me that I don't actually want an instance, I
want the real deal. But I have no idea how to make that happen.
How do I grab a hold of MyListItemArray and use it in the table data
source methods without instantiating it?
I read through some documentation about model objects and objects in
general, and stumbled upon the concept of Singletons. Some
additional searching lead me to this blog post:
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
...which seems to describe exactly what I want. I reconfigured
MyListItemArray as a singleton, and remapped my data source methods
accordingly, and everything seemingly works perfectly. Items added
in one view are displayed in the other, and so on. It works so well
that I have to assume there is a catch.
I can't shake the feeling that this seems more difficult than in
ought to be, and generally when I feel that way there tends to be a
single line of code solution that I haven't found yet. Are
singletons really the only way (that doesn't involve saving to a
file, I suppose) to share a model object across multiple view
controllers? I think I'm missing something really fundamental.
Thanks,
Brian
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden