I have a very simple question I hope someone can answer as I start understanding bindings. I want to programmatically change my NSString value and have the NSTextField update to that value thru bindings. I have a NSTextField and NSLabel. To represent the value of the myString properly changing I have a NSButton.
- I have NSTextField’s Value bound to myString property of App Delegate with Continuously Update Value checked.
- I have NSLabel’s Value bound to myString Property of App Delegate.
- I have NSButton outlet hooked to setDefault method.
When I type in NSTextField the NSLabel updates as expected but when I click the button the myString property is updated but not in the NSTextField.
What do I need to do to get the NSTextField update to the myString property????
AppDelegate.h
@interface AppDelegate : NSObject<NSApplicationDelegate>
{
NSString *myString;
}
@property (assign) IBOutlet NSWindow *window;
@property NSString *myString;
- (IBAction)setDefault:(id)sender;
@end
AppDelegate.m
@implementation AppDelegate
@synthesize window = _window;
@synthesize myString;
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
myString = @"This is a string";
}
- (IBAction)setDefault:(id)sender
{
NSLog(@"%@", myString);
myString = @"This is a string";
NSLog(@"%@", myString);
}
@end
It shouldn’t be
but this:
both in
-applicationDidFinishLaunching:and in-setDefault:. Don’t forget to specify self in your NSLog statements as well. You’d probably like to specify a different string in-setDefault:so that you can actually see that a change is taking place.One other thing: You’re effectively saying that you want to assign to myString, but that’s not appropriate for an object. Instead of:
you should instead use
or at least
The former is preferred because passing a
NSMutableStringinstance effectively copies it as aNSString, while passing aNSStringinstance simply retains it.Good luck to you in your endeavors.