0

Noob warning! My app displays data in a standard NSTableView via NSArrayController all via bindings. All cool.

There is a filterPredicate, “completed tasks” which is toggled by a check box which drives a BOOL in the background. This works great when the app is running, filter on, filter off, but if I set the BOOL to NO in either init or awakeFromNib, the NSArrayContoller ignores the filterPredicate, but works later.

I could use the predicate to deliver a filtered array to the AC, but guessed bindings & direct filterPredicate would be better.

The AC is init’d in the NIB file and from looking at the KVO feedback, gets created very early on. The filterPredicate does not exist when the AppController inits, but does by the time we get to awakeFromNIb.

Any ideas what I am missing?

Edit: This is the method called by the check box action:

- (void)displayClosedJobs {
if (self.showClosedJobs)
{
    self.showClosedPredicate=nil;
}
else
{
    NSString *predValue=@"Closed";
    self.showClosedPredicate=[NSPredicate predicateWithFormat:@"!(DisplayName contains[c] %@)",predValue];
}
[self changeTableSortOrder];}

This is also called on init and awakeFromNib.

Steve
  • 93
  • 7
  • Grrrr! Turned out to be a corrupt ArrayController object on my work bench in IB. Deleted, replaced and reconfigure the bindings and all works as expected. – Steve Feb 01 '13 at 11:50

1 Answers1

0

Most likely you set the bool variable in your init/awakeFromNib directly like so:

_myBoolVariable = NO;

However, what you have to do to send the correct KVO notifications for bindings to notice the change, is use the setter method explicitly either by

[self setMyBoolVariable:NO];

or by

self.myBoolVariable = NO;

However, it's hard to tell without seeing your initialization code. If this is not the problem you should provide more details about how you initialize the variables and how your bindings are set up.

Joachim Kurz
  • 2,875
  • 6
  • 23
  • 43
  • I’m setting the BOOL via the property method so I get the KVO goodness. What is odd that the BOOL via check box toggle works. – Steve Jan 11 '13 at 12:27
  • hm, what is the name of the property? what value do you bind the checkbox to? what exactly do you call in the awakeFromNib method? Where does the filterPredicate come from, how is it created/modified? Do you bind the NSArrayControllers filterPredicate property to it or how do you supply it? If so, what does this binding look like? – Joachim Kurz Jan 11 '13 at 13:12
  • There are 2 properties in the AppController class which are linked to BOOLs via the interface. One is filterPredicate, one is sortDescriptor(to reverse the table order). These properties are then linked to the AC via bindings and accessed by changing the properties in the AppController. – Steve Jan 11 '13 at 14:49
  • But the filterPredicate property of the NSArrayController class is of type NSPredicate, so I'm confused how you establish the binding to a BOOL? Could you add your @interface file (or excerpt thereof) of the AppController class showing how the properties are declared? Also a list of bindings (what is bound to what) would be great. – Joachim Kurz Jan 11 '13 at 15:29
  • In the interface I have: `@property BOOL showClosedJobs;` `@property (retain) NSPredicate *showClosedPredicate;` Sorry do not know the markdown to get code to look nice in a post. – Steve Jan 11 '13 at 16:52
  • The AC is bound to the AppController with the keyPath **`showClosedPredicate`**. The data source for the AC is a an NSArray in a custom class which pulls data from a MySQL db, which again is linked through bindings. – Steve Jan 11 '13 at 17:14
  • I assumed you would edit the question to include it ;-) (would have commented on the question if I were allowed to) – Joachim Kurz Jan 11 '13 at 18:30
  • @Steve I assume you bound the ArrayController's filterPredicate to the AppController's @"showClosedPredicate" keypath? And the checkboxe's value property is bound to the AppController's @"showClosedJobs" key? If so, how does setting the showClosedJobs property change the showClosedPredicate property? Did you implement a custom getter or setter? I assume you create the initial showClosedPredicate somewhere yourself? Or where does it come from? – Joachim Kurz Jan 11 '13 at 18:35
  • The `showClosedJobs` property is synthesised. The `-displayClosedJobs` method sets/kills the predicate. – Steve Jan 12 '13 at 15:37
  • Just built a little POC app where the AC was created in code rather than through IB with bindings and I get the desired results. I used NSTableViewDelegate and NSTableViewDatasource as opposed to bindings. It looks like there is either a bug with KVO or, more likely, I have just got the timings wrong for when it all gets updated. Thanks for your help. – Steve Jan 14 '13 at 10:46