Re: NSOutline and NSTreeController using bindings
Re: NSOutline and NSTreeController using bindings
- Subject: Re: NSOutline and NSTreeController using bindings
- From: Chris Hanson <email@hidden>
- Date: Fri, 15 Oct 2010 03:10:14 -0700
My apologies - I dashed off my last email to you too quickly and made a significant mistake in it.
I intended to suggest using -mutableArrayValueForKey: rather than -valueForKey:. For example:
- (void)awakeFromNib {
[[self mutableArrayValueForKey:@"myListRoot"] addObject:foo];
[[self mutableArrayValueForKey:@"myListRoot"] addObject:bar];
[[self mutableArrayValueForKey:@"myListRoot"] addObject:baz];
}
or
- (void)awakeFromNib {
NSMutableArray *myListRootProxy = [self mutableArrayValueForKey:@"myListRoot"];
[myListRootProxy addObject:foo];
[myListRootProxy addObject:bar];
[myListRootProxy addObject:baz];
}
These will ensure KVO notifications are sent to any observers of your object's "myListRoot" property when it is modified. Note that it's about modifying the property, not the array behind it.
-- Chris
On Oct 15, 2010, at 3:05 AM, Hrishikesh Murukkathampoondi wrote:
> Thank you for the suggestion. I changed the code to use KVC like you suggested but it still makes no difference. But doing what you said along with calls to willChangeValueForKey and didChangeValueForKey
>
> This worked:
>
> -(void) awakeFromNib
> {
>
> NSTreeNode *tn = [NSTreeNode treeNodeWithRepresentedObject:[NSString stringWithString:@"History"]];
> [self willChangeValueForKey:@"myListRoot"];
> [[self valueForKey:@"myListRoot"] addObject:tn];
> [self didChangeValueForKey:@"myListRoot"];
>
> }
>
>
> Even if I don't use KVC to add entries to myListRoot sending a reloadData to the NSOutlineView should refresh the contents correct? It does not work when I do [myOutlineView reloadData] after I update myListRoot.
>
> Style issues - I agree. But this is a example I am using to teach myself. It is not part of a larger project.
>
> Hrishi
>
>
> On 13-Oct-2010, at 12:43 AM, Chris Hanson wrote:
>
>> This implies that you’re not manipulating your “myListRoot” property in a way compliant with Key-Value Observing.
>>
>> Just manipulating the instance variable will not post KVO notifications for the property. You need to manipulate the property (for example, by working with the proxy NSMutableArray returned by [self mutableArrayValueForKey:@"myListRoot"]) in a KVO-compliant fashion for bindings to notice your changes to it.
>>
>> In other words, I think your -awakeFromNib code probably looked like this:
>>
>> - (void)awakeFromNib {
>> myListRoot = [[NSArray alloc] initWithObjects:foo, bar, baz, nil];
>> }
>>
>> It should look like this:
>>
>> - (void)awakeFromNib {
>> [[self valueForKey:@"myListRoot"] addObject:foo];
>> [[self valueForKey:@"myListRoot"] addObject:bar];
>> [[self valueForKey:@"myListRoot"] addObject:baz];
>> }
>>
>> The reason it may have worked in -init is that when your bindings set up KVO for the "myListRoot" property, they may have retrieved its initial value.
>>
>> Also, just on a stylistic note, I wouldn't name a property something like "list" in a Cocoa application to represent a collection presented in an outline view; Cocoa's controls are "tables" and "outlines" rather than "lists" and "trees." (NSArrayController and NSTreeController use the terms they do because they're about the structure of the data presented, not the view; you can bind either an NSOutlineView or an NSBrowser to an NSTreeController, for example.) Ideally I'd name the property something more related to what the data actually represents, e.g. "people" or "products."
>>
>> -- Chris
>>
>>
>> On Oct 12, 2010, at 6:52 AM, Hrishikesh Murukkathampoondi wrote:
>>
>>> I moved the code populating myListRoot to the "-init" method of MyDocument.m and now it works. I was earlier populating it in "-awakeFromNib".
>>>
>>> I am so tried putting it back in -awakeFromNib followed by a call to [mOutlineView reloadData] - but this did not work.
>>>
>>> So I have my NSOutlineView showing me the text stored in my data root tree. But cant explain the above behavior.
>>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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