Bindings: problem changing model programmatically

  • I'm having trouble with Cocoa Bindings. I'm trying to set the model
    array programmatically. I'm trying to do this in Scott Stevenson's
    MailDemo example. I have this:

    - (IBAction)changeArray:(id)sender {
    [[self mutableArrayValueForKey:@"_mailboxes"] addObject:[[Mailbox
    alloc] init]];
    }

    which should be KVO compliant. What happens is that it doesn't show up
    until another change is made, like clicking the + or - in the
    interface to add or remove a mailbox.

    The following code has the same effect. willChangeValueForKey,
    didChangeValueForKey, and reloading the table view don't solve my
    problem.

    - (IBAction)changeArray:(id)sender {
    [self willChangeValueForKey:@"_mailboxes"];
    [[self mutableArrayValueForKey:@"_mailboxes"] addObject:[[Mailbox
    alloc] init]];
    [self didChangeValueForKey:@"_mailboxes"];
    [mailboxesTableView reloadData];
    }

    The bindings example is here:
    http://cocoadevcentral.com/articles/000080.php

    The finished version of that example is here, and is the project I
    started with:
    http://cocoadevcentral.com/articles/000084.php

    What am I missing here?

    Thanks!
    Steve
  • On Jan 18, 2008, at 2:26 PM, Stephen Zyszkiewicz wrote:

    > I'm having trouble with Cocoa Bindings. I'm trying to set the model
    > array programmatically. I'm trying to do this in Scott Stevenson's
    > MailDemo example. I have this:
    >
    > - (IBAction)changeArray:(id)sender {
    > [[self mutableArrayValueForKey:@"_mailboxes"] addObject:[[Mailbox
    > alloc] init]];
    > }
    >
    > which should be KVO compliant. What happens is that it doesn't show
    > up until another change is made, like clicking the + or - in the
    > interface to add or remove a mailbox.

    Your iVar's name is _ mailboxes, but the KVC/KVO key is "mailboxes"
    with no leading underscore.

    Jim
  • That was exactly the problem! I got that working in MailDemo and my
    own project!

    Thank you!
    Steve

    On Jan 18, 2008, at 3:14 PM, Jim Correia wrote:

    > On Jan 18, 2008, at 2:26 PM, Stephen Zyszkiewicz wrote:
    >
    >> I'm having trouble with Cocoa Bindings. I'm trying to set the model
    >> array programmatically. I'm trying to do this in Scott Stevenson's
    >> MailDemo example. I have this:
    >>
    >> - (IBAction)changeArray:(id)sender {
    >> [[self mutableArrayValueForKey:@"_mailboxes"] addObject:[[Mailbox
    >> alloc] init]];
    >> }
    >>
    >> which should be KVO compliant. What happens is that it doesn't show
    >> up until another change is made, like clicking the + or - in the
    >> interface to add or remove a mailbox.
    >
    > Your iVar's name is _ mailboxes, but the KVC/KVO key is "mailboxes"
    > with no leading underscore.
    >
    > Jim
    >
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today