Re: Binding NSPopupButton to a set of NSObjects
Re: Binding NSPopupButton to a set of NSObjects
- Subject: Re: Binding NSPopupButton to a set of NSObjects
- From: Fritz Anderson <email@hidden>
- Date: Thu, 2 Feb 2006 15:31:47 -0600
Don't start new threads by replying to other people's questions. It
hides your message and gives people the false impression that the
original poster had gotten an answer.
On 2 Feb 2006, at 1:15 PM, Bob Peterson wrote:
This is a rank newbie question, but the documentation and list
archives just aren't helping me. What I want to do is display a
set of arbitrary objects and know which object was selected. Using
selectedIndex seems more than a little indirect and hokey. How do
I directly see the selection as one of the objects that is part of
the displayed set?
#include "B.h"
@interface A : NSObject {
B* currentB;
NSSet* allBs;
}
Don't use #include in Objective-C. Use #import.
Don't cross-include @interface files in .h files. Use the @class
directive to indicate that a symbol refers to a class, and do the
#import in the corresponding .m file.
I bet you care what order the contents of allBs come in: You would
not, for instance, be pleased if a popup menu built from allBs
changed order as the underlying set changed. Additionally, you are
going to use an NSArrayController, which controls arrays, not sets.
You mean NSArray, not NSSet.
@interface B : NSObject {
NSString* name;
//...plus many other instance variables...
}
In my nib I have an instanceA1 which is an A. Using IB, I want to
bind an NSPopupButton to instanceA1's allBs property. I'll display
each B's "name" property in the popup. Any time I want to know
what the currently selected B is, I find out by just looking at
currentB, which has been bound to that NSPopupButton, too,
somehow. And more, when I change currentB I want that to change
the selection in the NSPopupButton.
But I just can't grok which bindings get bound where.
Your problem is that classes A and B are model objects, and you are
trying to make them do controller things. Bindings extend from
controller objects, both up to the view layer and down to the model
layer. Your nib should include an NSObjectController; if it is
convenient to include an instance of A in the nib, then the
NSObjectController's content outlet should be connected (as in
control-dragging) to the A instance, otherwise you'll have to do a
setContent: at run time.
Your nib should include an NSArrayController whose contentArray is
bound to the allBs property of the NSObjectController. The
NSArrayController will track the selected B.
Bind the NSPopupMenu's contentValues to the "name" property of the
arrangedObjects of the NSArrayController. Bind the NSPopupMenu's
selectedValue to the name of the selection of the NSArrayController.
(It may be that you have to bind the content property of the popup to
the arrangedObjects of the controller, and bind selectedObject to the
controller's selection.) I'm working from memory, so you may have to
fish around, under the guidance of the documentation:
For example, I can't find anything in the documentation to explain
what is the 'attribute' (or is it a 'property') named
"contentObjects", as seen in Interface Builder's bindings inspector
for an NSPopupButton.
In Xcode, open the Documentation window. Select "Full-Text Search"
from the popup in the search field. Type "contentObjects" into the
search field. Discover that "NSPopupButton Bindings" is the second of
five documents answering that query.
-- F
--
Fritz Anderson -- http://www.manoverboard.org/
Consulting Programmer -- http://resume.manoverboard.org/
Step into Xcode, Now Available -- http://six.manoverboard.org/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden