NSObjectController confusion binding to a class property. Help!
- by scottw
Hi,
I'm teaching myself cocoa and enjoying the experience most of the time. I have been struggling all day with a simple problem that google has let me down on. I have read the Cocoa Bindings Program Topics and think I grok it but still can't solve my issue.
I have a very simple class called MTSong that has various properties. I have used @synthesize to create getter/setters and can use KVC to change properties. i.e in my app controller the following works:
mySong = [[MTSong alloc]init];
[mySong setValue:@"2" forKey:@"version"];
In case I am doing something noddy in my class code MTSong.h is:
#import <Foundation/Foundation.h>
@interface MTSong : NSObject {
NSNumber *version;
NSString *name;
}
@property(readwrite, assign) NSNumber *version;
@property(readwrite, assign) NSString *name;
@end
and MTSong.m is:
#import "MTSong.h"
@implementation MTSong
- (id)init
{
[super init];
return self;
}
- (void)dealloc
{
[super dealloc];
}
@synthesize version;
@synthesize name;
@end
In Interface Builder I have a label (NSTextField) that I want to update whenever I use KVC to change the version of the song. I do the following:
Drag NSObjectController object into the doc window and in the Inspector-Attributes I set:
Mode: Class
Class Name: MTSong
Add a key called version and another called name
Go to Inspector-Bindings-Controller Content
Bind To: File's Owner (Not sure this is right...)
Model Key Path: version
Select the cell of the label and go to Inspector
Bind to: Object Controller
Controller Key: mySong
Model Key Path: version
I have attempted changing the Model Key Path in step 2 to "mySong" which makes more sense but the compiler complains. Any suggestions would be greatly appreciated.
Scott
Update Post Comments
I wasn't exposing mySong property so have changed my AppController.h to be:
#import <Cocoa/Cocoa.h>
@class MTSong;
@interface AppController : NSObject {
IBOutlet NSButton *start;
IBOutlet NSTextField *tf;
MTSong *mySong;
}
-(IBAction)convertFile:(id)sender;
@end
I suspect File's owner was wrong as I am not using a document based application and I need to bind to the AppController, so step 2 is now:
Go to Inspector-Bindings-Controller Content
Bind To: App Controller
Model Key Path: mySong
I needed to change 3. to
Select the cell of the label and go to Inspector
Bind to: Object Controller
Controller Key: selection
Model Key Path: version
All compiles and is playing nice!