• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Bindings + contents of an array. No dice, for newb.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bindings + contents of an array. No dice, for newb.


  • Subject: Bindings + contents of an array. No dice, for newb.
  • From: Shamyl Zakariya <email@hidden>
  • Date: Tue, 3 Apr 2007 17:47:50 -0400

I'm new to bindings, and I can't figure out how to get it to notice changes to the elements contained in an array. So I've written a very very simple app to try to figure out what's going on.

1) I have an object "Thing" which has an array of "Value" instances ( "values" ), where each Value has a single string property, "value".

2) Thing has a method "output" which returns a string which is the comma separated concatenation of all the Value instances' -value strings.

The Thing -output method looks like so:
- (NSString *) output
{
	NSString *output = nil;
	NSEnumerator *enumerator = [_values objectEnumerator];
	Value *v = nil;

	while( v = [enumerator nextObject] )
	{
		if ( output )
			output = [output stringByAppendingFormat: @", %@", [v value]];
		else
			output = [v value];
	}

	return output;
}


3) The constructor of Value sets its value to "ValueN" where N is an increasing number ( e.g., Value0, Value1, Value2.. )


4) Finally, in Thing's +initialize I set the following dependency to make -output dependant on -values:
+ (void) initialize
{
[self setKeys:
[NSArray arrayWithObjects:
@"values",
@"values.value",
nil]
triggerChangeNotificationsForDependentKey: @"output"];
}


In IB I made an NSObjectController "ThingAlias" who's content is bound to an instance of Thing. I then bound a read-only text field to ThingAlias.selection.output. Then I made an NSArrayController "Values" and bound it to ThingAlias.selection.values, and bound a table view with one column to display Values.arrangedObjects.value. Finally, I have +/- buttons to add and delete the entries in the array.

So, when I run this, each time I hit "+", the result field shows "Value0", then "Value0, Value1", then "Value0, Value1, Value2" and so on. This is what I expect. Hitting "-" removes the newest from the array and the output field reflects this.

Now, when I double click one of these Value instances, and type a new name, I'd expect on committing it ( hitting enter ) to see the output field updated. But it doesn't!

However, if I hit "+" or "-", the dependancy is triggered and I see the renamed value show up in the results field. So, clearly changing the *array* triggers the notification, but changing the values in an object owned by the array does not.

How can I get bindings to notice changes to the contents of the array, not just the array instance itself?

email@hidden
    "It's all malarky; even the wonderful part is malarky"
        --Robert Stack


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


  • Follow-Ups:
    • Re: Bindings + contents of an array. No dice, for newb.
      • From: Shamyl Zakariya <email@hidden>
  • Prev by Date: Re: NSView autoresizing
  • Next by Date: Re: NSView autoresizing
  • Previous by thread: Re: NSView autoresizing
  • Next by thread: Re: Bindings + contents of an array. No dice, for newb.
  • Index(es):
    • Date
    • Thread