Why is NSArrayController not calling accessor methods via bindings?
I am trying to intercept an NSArrayController (thingsController) addObject method with the following accessor method.
- (void)insertObject:(Thing *)thing
inThingsAtIndex:(NSUInteger)index;
I have two classes: Thing and MyDocument. Thing has one property: name. MyDocument has an NSMutableArray called things and an NSArrayController called thingsController. In the NIB, File's Owner is set to MyDocument and I have the NSArrayController's content array bound to File's Owner and the model path set to things. The NSArrayController also has mode set to Class, Class Name set to Thing and it has one key called name. In MyDocument I have a method called createThing, which first sends thingsController newObject and then sends it addObject. If I set a breakpoint in the init method in Thing, it gets called when thingsController is sent newObject. However, when thingsController is sent addObject, my accessor method insertObject:(Thing *)thing inThingsAtIndex:(NSUInteger)index is not called.
I have read Apple's documentation on Key-Value Coding Accessor Methods and I think I'm compliant, however, I must be missing something.
Any help would be greatly appreciated.
Code below...
Thing.h
#import <Cocoa/Cocoa.h>
@interface Thing : NSObject {
NSString *name;
}
@property (readwr开发者_JAVA百科ite, copy) NSString *name;
@end
Thing.m
#import "Thing.h"
@implementation Thing
@synthesize name;
-(id) init
{
[super init];
name = @"Default";
return self;
}
@end
MyDocument.h
#import <Cocoa/Cocoa.h>
@class Thing;
@interface MyDocument : NSDocument
{
NSMutableArray *things;
IBOutlet NSArrayController *thingsController;
IBOutlet NSTableView *tableView;
}
- (IBAction)createThing:(id)sender;
- (void)insertObject:(Thing *)thing
inThingsAtIndex:(NSUInteger)index;
- (void)removeObjectFromThingsAtIndex:(NSUInteger)idx;
@end
MyDocument.m
- (IBAction)createThing:(id)sender
{
//Create the object
Thing *t = [thingsController newObject];
//Add it to the content array of 'thingsController'
[thingsController addObject:t];
NSLog(@"The new content of array is%@",things);
[t release];
}
Apologies. I made one change and it works now. I thought I had tried this in the past and it had not worked...
I added
@property (readwrite, copy) NSMutableArray *things;
to MyDocument.h
and
@synthesize things;
to MyDocument.m.
Seems to be working.
Regards.
精彩评论