Skip navigation.
 
mlRe: Simple bindings problem
FROM : mmalc crawford
DATE : Sun Jan 06 10:58:21 2008

On Jan 6, 2008, at 12:22 AM, Martin Linklater wrote:
> I just thought there may be a way to do it without writing any code
>

I'm not sure how many times it's been said now, but to reiterate, 
"Sometimes you have to write some code".

> After googling for a bit last night I found this on CocoaDev:

>>

> There are a number of ways to customize new objects that get added 
> to the array:
>


Indeed.
And which you choose depends on where it makes most sense for your 
particular application.  There may be no Right Answer.

> Subclass NSArrayController and override newObject or addObject.
> Register as an observer of the array's key in the providing object, 
> using KeyValueObserving. You will be informed when a new object is 
> added to that array, which you can then modify (the new object will 
> be passed to you.)
> In the object providing the content array, implement the 
> KeyValueCoding array methods, one of which is 
> insertObject:in<Key>AtIndex?:.
>

Or you could use the straightforward "traditional" way.  Connect your 
Add button to the controller you already have in your nib (typically 
your File's Owner in a document-based application, otherwise perhaps 
your App Controller) and put the custom logic in there, just as you 
always would have...
Your only constraint then is that you add the new object to the array 
in a KVO-compliant manner -- or add it directly to the array controller.

>> Well, how do you do it currently?

>
> Currently in my test app I have an NSPopUpMenu, an NSTextField next 
> to it, and two NSButtons (Add and Remove). My NSPopUpMenu is bound 
> to my array controller, which has it's contents initialised in my 
> AppControllers init method. This inits the NSPopUPMenu with 4 
> strings. I have the Add and Remove buttons triggering the insert: 
> and remove: actions of the array controller.

>>
>> How do you want to do it?

>
> What I would like is for whatever string I enter into the 
> NSTextField to be used as the string for the new NSPopUpMenu entry.
>

Presumably your App Controller could have outlets to the text field 
and the array controller...?
Or you could subclass NSArrayController to add an outlet to the text 
field...

mmalc

Related mailsAuthorDate
mlSimple bindings problem Martin Linklater Jan 5, 10:58
mlRe: Simple bindings problem Martin Linklater Jan 5, 12:46
mlRe: Simple bindings problem Martin Linklater Jan 5, 23:02
mlRe: Simple bindings problem mmalc crawford Jan 6, 01:24
mlRe: Simple bindings problem Martin Linklater Jan 6, 09:22
mlRe: Simple bindings problem mmalc crawford Jan 6, 10:58
mlRe: Simple bindings problem Martin Linklater Jan 6, 14:13