Skip navigation.
 
mlBoggled By Bindings
FROM : Patrick Machielse
DATE : Thu Sep 30 22:32:45 2004

I want to bind to a new ivar of my NSArrayController subclass, but somehow
the change notifications never get send.

What I have:

In my NSArrayController subclass I have an 'infoString' method that returns
an array, depending on the length of 'arrangedObjects'. I have also
implemented +initialize to send a change notification for infoString when
arrangedObjects change.

+ (void)initialize
{
    [[self class] setKeys:[NSArray arrayWithObject:@"arrangedObjects"]
                  triggerChangeNotificationsForDependentKey:@"infoString"];
}

- (NSString *)infoString
{
    int  n = [[self arrangedObjects] count];
    if ( n == 0 )
        return @"no objects";
    else {
        NSString *fmt = @"%d object%@";
        return [NSString stringWithFormat:fmt, n, n > 1 ? @"s" : @""];
    }
}

In IB I bind an NSTextField's value to

        Bind to:(my NSArrayController subclass instance)
  Controller Key:infoString
  Model Key Path:<empty>

This works, but _only once_; at startup the textfield is set to 'no
objects', and subsequent changes to arrangedObjects don't have any effect.
Actually, it doesn't matter what keys I specify for 'triggerChange...'

After much experimenting I tried adding

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key;
{
    if ( [key isEqualToString:@"infoString"] ) {
        return YES;
    }
    return [super automaticallyNotifiesObserversForKey:key];
}

since it seems NSArrayController doesn't return the default 'YES' value for
all keys (is this documented, somewhere?). Still, no success.

Then, (getting more desperate..) I tried adding the following code to
+initialize

    [[self class] exposeBinding:@"infoString"];

Not sure what exactly that would do, but it wouldn't hurt to try. Again no
luck.

I even tried binding to 'infoString' through a separate NSObjectController.
(yes, desperate!) Alas...

So it seems that there is something preventing me to bind to a new ivar of
an NS(Array)Controller subclass. Or maybe I just missed an essential
ingredient?

Any help or illumination is highly appreciated!


N.B.
I worked around this problem by binding the textfield to 'arrangedObjects'
and creating a value transformer to generate the info string. This works
fine, but I'm still curious to know why binding straight to the
NSArrayController ivar fails...


Patrick
---
Hieper Software

w:  www.hieper.nl
e:  <email_removed>

Related mailsAuthorDate
mlBoggled By Bindings Patrick Machielse Sep 30, 22:32
mlRe: Boggled By Bindings Jacob Lukas Oct 1, 00:55
mlRe: Boggled By Bindings Scott Stevenson Oct 1, 01:14
mlRe: Boggled By Bindings Scott Stevenson Oct 1, 01:20
mlRe: Boggled By Bindings Jacob Lukas Oct 1, 01:26
mlRe: Boggled By Bindings Scott Stevenson Oct 1, 02:46
mlRe: Boggled By Bindings Jacob Lukas Oct 1, 03:09
mlRe: Boggled By Bindings Scott Stevenson Oct 1, 05:05
mlRe: Boggled By Bindings Jacob Lukas Oct 1, 08:18
mlRe: Boggled By Bindings Christian Brunsche… Oct 1, 11:03
mlRe: Boggled By Bindings Patrick Machielse Oct 1, 11:24
mlRe: Boggled By Bindings Patrick Machielse Oct 1, 13:26
mlRe: Boggled By Bindings Patrick Machielse Oct 1, 13:35
mlRe: Boggled By Bindings Eric Ocean Oct 1, 17:37
mlRe: Boggled By Bindings Scott Stevenson Oct 1, 23:38
mlRe: Boggled By Bindings Scott Stevenson Oct 1, 23:40
mlRe: Boggled By Bindings Patrick Machielse Oct 4, 13:56
mlRe: Boggled By Bindings Steven Kramer Oct 4, 17:09
mlRe: Boggled By Bindings Patrick Machielse Oct 6, 12:57
mlRe: Boggled By Bindings Steven Kramer Oct 7, 15:02