Reputation: 2026
Cocoa bindings, KVC, and KVO are starting to make my head hurt. All I want to do is have an NSTextField's value bound to the value of a property of my view controller. Could someone tell me where I'm going wrong? Any help would be greatly appreciated. Below is a simplified version of what I've got going on.
MyViewController.h:
#import <Cocoa/Cocoa.h>
@interface MyViewController : NSViewController
@property NSString *colorSpaceName;
@property IBOutlet NSTextField *colorSpaceLabel;
@end
MyViewController.m:
#import "MyViewController.h"
@implementation MyViewController
@synthesize colorSpaceName;
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
// ...
if ( self ) {
[self.colorSpaceLabel bind:@"stringValue"
toObject:self
withKeyPath:@"colorSpaceName"
options:nil];
}
// ...
}
@end
Upvotes: 0
Views: 213
Reputation: 4729
According to IB there is no 'stringValue" binding for NSTextField
just a 'value' binding. Unless you are setting up your UI all in code the easiest thing to do is to use IB for bindings.
Select the NSTextField
in the xib file. Then select the bindings tab in the utilities area to the right. The first binding listed should be value
, expand it. From the popup menu select "File's Owner" as the object to bind to. Xcode will enter self
into the Model Key Path field for you, just add .colorSpaceName
to the end of the field and press return.
If you really must do the binding in code then Change @"stringValue"
to @"value"
and make sure that your outlet is connected in IB.
Note: If you are creating the UI in code there is no need to declare any of the elements as IBOutlet
since that and IBAction
are just keywords for IB to know what properties and methods to pay attention to.
Upvotes: 1