Cocoa auto-update dictionary values from NSTextField
Right now I have a dictionary containing a string value and an NSTextField in my interface. However, in order to update this开发者_开发问答 value I have to click a button that then runs the update code. How can I make it dynamically update anytime the text field's value changes?
Look into using Cocoa Bindings.
They're designed to keep your view (NSTextField) in sync with your model (dictionary) without writing all the glue code in between. They're a bit tricky to learn, but once you understand them, they're super useful.
In your case, you'd bind the "value" binding of the NSTextField to a property in your code.
An alternative is to set up an NSTextFieldDelegate and implement:
- (void)controlTextDidChange:(NSNotification *)aNotification
to modify the value in the dictionary. For example,
- (void)controlTextDidChange:(NSNotification *)aNotification {
[myDictionary setValue:[myTextField stringValue] forKey:@"MYDictionaryKey"];
}
Now whenever the user modifies the text in the NSTextField, the text field will fire this callback to its delegate. This way, you can make sure the dictionary always has the same value as what's displayed on screen.
If you only want the changes to take effect when the user is done editing, you'd implement:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
精彩评论