Cocoa/Objective-C messaging between views, table and array controllers
I've been a .NET developer for many years but just getting started in Objective-C/Cocoa. I've worked through the Hillegass book along with Scott Stevens Up and Running. Learned a lot over the past few weeks (in large part due to this site) and开发者_如何学运维 I've been writing an app that I had previously written in VS. It uses core data and tracks my large library of sheet music. The app is comprised of a main window with a horizontal split view. Top view has three tables for criteria selection so I can narrow down what shows in the lower view such as music type, decade and genre (for example ballad, 80's and rock).
I have managed to build the main view and have the selections create a predicate which is bound to the filer predicate of the array controller in the lower view. The lower view is a custom view with it's own nib. When the selections in the top view change, the predicate gets rebuilt but the only way I can seem to get it to apply to the table in the lower view is to reload the view. That would be OK except for one thing. The lower view also has two tables with a master-detail type interface showing artist on the left and songs on the right. So if I have an artist selected in the lower view I want that selection to persist when I change the selection criteria in the upper view. If I reload the lower view I lose the artist selection in the lower view.
So after that mini novel here's the question. How do I send a message from the main view to update the filter predicate and refresh the songs table without losing the focus. I know there has to be a way but I can't for the life of me find it. I have a custom class to build and return the predicate and I can instantiate the class and get the predicate which I first thought would be enough since the filter predicate is bound to that class but I guess doing that creates a new instance of the class and the binding is not seeing it.
Any help would be greatly appreciated.
If you want to bind one objects properties to another object (automatically update the bound object when the source object changes values), I recommend looking into Cocoa Bindings
For instance you can bind one object easily to another objects properties with
-bind:toObject:withKeyPath:options:
E.g.
[self bind:@"myDependendProperty"
toObject:myOtherView
withKeyPath:@"myOtherViewsKeyPath" // this can be for instance @"frame", @"bounds"...
options:myBindingOptionFlags];
If you don't need automatic updates you can just stick with the NSNotificationCenter
and post the objects there with your own defined notification name (can be an arbitrary string).
精彩评论