Re: NSArrayController KVO question...
Re: NSArrayController KVO question...
- Subject: Re: NSArrayController KVO question...
- From: Randy Widell <email@hidden>
- Date: Wed, 21 Nov 2012 14:19:26 -0800
Hmm. Well, I am updating the model (and the database that backs it). I just need the UI to reflect it.
I guess I was assuming that NSArrayController would respond to a value observation the same way it responds to, for instance, a selection change in the table view (updates the menu item's check mark).
So, if the window controller has to catch the observation, what's the proper way to have the array controller update the UI?
I tried removing the objects affected and adding them back in with the changed value which resulted in some weirdness while iterating over selectedObjects. Not sure I want to re-query the database to update the full contents of the array controller either...unless it's necessary.
On Nov 21, 2012, at 13:46, "email@hidden" <email@hidden> wrote:
>
> On 21 Nov 2012, at 20:42, Randy Widell <email@hidden> wrote:
>
>> Searching around the Internet, I see a lot of questions and answers about how to observe changes to properties of objects in a NSArrayController. Everything I have seen, though, relates to a third object doing the observing. For instance:
>>
>> [arrayController addObserver: viewObject forKeyPath: @"arrangedObjects.someProperty" ... ]
>>
>> I am trying to do something similar, but I want to notify the array controller itself.
>>
>> I have an array controller that drives a table view, enables/disables an action button, and updates check marks for menu items in the action button's drop down menu. That all works fine.
>>
>> When I select an action, I want to change a property and update all of the array controller's bindings (update the table view to reflect the change, update the drop down menu's check mark). For example, I have an "archive" action that should update the item's cell in the table view to display the "archived" graphic and "Archive" should be checked in the drop down menu.
>>
>> I followed the Hillegass example of adding an observer whenever an item is added to the array controller and removing the observer whenever an item is removed from the array controller. So, I have...
>>
>> - (void) startObserving: (id) object
>> {
>> [object addObserver: arrayController forKeyPath: @"someProperty" ... ];
>> }
>>
>> - (void) stopObserving: (id) object
>> {
>> [object removeObserver: arrayController forKeyPath: @"someProperty" ... ];
>> }
>>
>> The idea being that the array controller is notified whenever someProperty changes. However, nothing happens when I change the property.
>>
>> I used my window controller as the observer instead of the array controller, added an observeValueForKeyPath:... method and verified that changing the property of an object does, in fact, trigger an observeValueForKeyPath:... call.
>>
>> So, two questions: 1) is it even the correct approach to have the array controller observe changes in order to update bindings?, 2) if it is, is there something I am missing?
>> _______________________________________________
> Do you really want to update the arrayController bindings rather than update the model?
> If you want to have the tableview reflect the model's archive property then set it on the model.
>
> The reason your observation isn't working is because the KVO notification is being sent to the NSArrayController instance and it's default implementation of - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context (if it exists) will ignore it . You would have to subclass NSArrayController.
>
> The window controller implementation works because you can catch - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context.
>
> There is no reason not to let the window controller catch the observation and reconfigure the NSArrayController accordingly.
>
> Regards
>
> Jonathan Mitchell
> Mugginsoft LLP
>
> ================================================
> KosmicTask - the Integrated Scripting Environment for OS X.
> http://www.mugginsoft.com/KosmicTask
> ================================================
> _______________________________________________
>
> 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