Re: NSArrayController access between nib files
Re: NSArrayController access between nib files
- Subject: Re: NSArrayController access between nib files
- From: Brian Carmalt <email@hidden>
- Date: Mon, 10 Aug 2009 20:20:53 +0200
Sorry to Quincey, I sent the reply to him and not to the list.
Hello Quincey,
Thanks for your answer.
I am trying to figure out how to access a NSArrayController in one
nib file from another nib. I have done the NSPersistentDocument
tutorial and I am trying to apply what I have learned.
In general, it's not a great idea. It just makes the two nib files
more tangled up than they need to be, for no real benefit. It seems
better to have separate array controllers for different contexts,
and to bind them to the same data model array if that's what they're
supposed to show.
Ok, point taken. I'm just starting to learn, and have extended the
example in the NSPersistentDocument tutorial.
So in my MyDocument.nib there is a array controller I would like to
bind to a NSComboBox from a NewObjectSheet.nib. I have a
NewObjectSheetController in MyDocument.nib and the same controller
is file's owner in the NewObjectSheet. The controller has an outlet
which points to an array controller which I would like to bind to
an NSComboBox in the NewObjectSheet.nib.
I don't think you mean what you say here. It sounds like you want to
bind the combo box to the array controller, not the array controller
to the combo box.
Yep, I described it backwards.
First I'm not sure if it is possible to bind the NSComboBox to the
NewObjectSheetController's outlet to the array controller.
Content would be bound to File's Owner with a model key path of
self.arrayController
Content Values would be bound to File's Owner with a model key path
of self.arrayController.arrangedObjects
Value be bound to File's Owner with a model key path of
self.arrayController.selection.name
Well, all that *would* be fine, though incidentally you don't need
"self." in any of the bindings. The only time you need to use "self"
is when the model key path would otherwise be empty and you're
required to put something there.
Also, you cannot bind anything to an outlet. You only bind to a
property of an object. As it happens, in many cases, the KVC
mechanism is happy to treat an instance variable (such as an outlet)
as if it were a property of the same name, but it's not recommended
to rely on this any more. Much better to define the properties you
want in NewObjectSheetController, even if the properties just
encapsulate the values of instance variables.
However, without knowing anything further about what you're trying
to achieve, it would almost certainly be wrong to use this set of
bindings. It looks like you're trying to use the combo box as a kind
of "chooser" that picks one thing out of an array of things that the
array controller supplies. IOW, you're expecting a combo box to be a
kind of menu, but it's not -- it's a kind of text field. You should
probably be using a NSPopUpButton instead, which *is* a kind of menu.
Let's say I want the combo box to provide the user with auto
completion on the names of categories in the array controller, and if
the user types in a category name that does not exist, then they will
be automatically prompted to create the category. This will not work
with a popup button. Will this work? or are there known pitfalls I
should look out for?
If the scenario above is not possible, what is the best way to set
this up? Should I set up a new NSArrayController in the
NewObjectSheet.nib, set its entity to the same entity in the array
controller I would like to use and then use the
NewObjectSheetController access to the Document's
ManagedObjectContext(MOC) and bind the MOC of the array controller
to the document MOC and then use the bindings in the normal way?
I'm not a big fan of putting window controllers
(NewObjectSheetController) in a nib file. It means you unnecessarily
create the window controller every time you open a document, whether
or not you ever end up using the sheet. It also prevents you from
communicating document-related state to the window controller's
initialization, so you may end up inventing a means of communication
between the document and the window controller that's more
complicated than just having the document create the window
controller when it needs it (and you end up preventing the window
controller and all of its associated resources from being released
when the sheet is closed).
Ok, that's good to keep in mind. I'm just not that advanced yet. Your
first sentence in your mail tells me that my second option is the
better of the two. I should create a new array controller in my
NewObjectSheet context and set it up to use the documents MOC and bind
my combo box to this new array controller, that is if I can use the
NSComboBox like I described above.
Thanks again for your answer.
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