Re: How to manage creation of an unknown number of windows?
Re: How to manage creation of an unknown number of windows?
- Subject: Re: How to manage creation of an unknown number of windows?
- From: Jens Alfke <email@hidden>
- Date: Mon, 5 Oct 2009 16:37:55 -0700
On Oct 5, 2009, at 7:20 AM, Rui Pacheco wrote:
I've a class that extends NSWindowController and that class lists an
unknown
number of entries. Double clicking on an entry will open a window
where it
will be possible to interact with the real world object that entry
represents.
I plan to make the editing window extends NSDocument
NSDocument assumes every document is a file. If the real-world objects
you're using aren't individual files, you won't be able to use
NSDocument. Instead you can use NSWindowController, and have each
instance manage one of these objects.
but I am still at a
loss on something: how do I make sure manage the creation of an
unknown
number of windows on my app?
Creating the windows is easy; you just instantiate another of your
controllers with the data object as a parameter, and it opens another
instance of the nib.
I think what you're asking about is how to find the window associated
with a particular object. Two ways to do this:
[1] Create a global NSMutableDictionary that maps objects (or their
unique identifier strings) to controllers. The controller adds its
object to the dictionary when it's created, and removes itself when
the window's closed. (You can't put the remove code in the
controller's -dealloc method, because the reference from the
dictionary will keep the controller's refcount from going to zero.)
[2] Write a function that walks the window list. For each window, it
checks whether its delegate is an instance of your controller class.
If so, it looks at the controller's model object, and if it's the one
being looked for, returns the controller.
I usually use method #2 because it requires less bookkeeping.
—Jens
_______________________________________________
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