I am new to Cocoa but I think you guys have this wrong. You should not be adding to or updating the binding context you should be adding to the controller the context is bound to. E.g. in the case of an NSArrayController such as the CarsArrayController example in Hillegass's book. In this example imagine you had an NSTimer and wanted to add a new entity on the fire event every second. In MyDocument.m you would do:
- (id)init
{
self = [super init];
if (self != nil) {
// initialization code
// maybe this should go in awakeFromNib?
timer =[ [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(myTimerFireMethod:)
userInfo:nil
repeats:YES] retain];
}
return self;
}
- (void)myTimerFireMethod:(NSTimer*)theTimer{
id o = [carArrayController newObject];
[o setValue:@"Ford" forKey:@"makeModel"];
[carArrayController addObject:o];
}
Finally of course you need to hook up the referencing outlet in the CarArrayController object you added in interface builder to the carArrayController field of File's Owner. So right click the Cars icon and drag from the "New Referencing Outlet" to Files Owner and click carArrayController in the pop up.
So when you add to the array controller in code the UI is instantly updated because all of the binding stuff works as normal. Please tell me if I am wrong about this.
by Malcolm Hall — Jul 19
- (id)init { self = [super init]; if (self != nil) { // initialization code // maybe this should go in awakeFromNib? timer =[ [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myTimerFireMethod:) userInfo:nil repeats:YES] retain]; } return self; } - (void)myTimerFireMethod:(NSTimer*)theTimer{ id o = [carArrayController newObject]; [o setValue:@"Ford" forKey:@"makeModel"]; [carArrayController addObject:o]; }
and in MyDocument.h you have:
#import <Cocoa/Cocoa.h> #import "CarArrayController.h" @interface MyDocument : NSPersistentDocument { NSTimer* timer; IBOutlet carArrayController* c; } - (void)myTimerFireMethod:(NSTimer*)theTimer; @end
Finally of course you need to hook up the referencing outlet in the CarArrayController object you added in interface builder to the carArrayController field of File's Owner. So right click the Cars icon and drag from the "New Referencing Outlet" to Files Owner and click carArrayController in the pop up.
So when you add to the array controller in code the UI is instantly updated because all of the binding stuff works as normal. Please tell me if I am wrong about this.