I can’t figure out how to get an NSTextfield to update automatically, without having to press “Return” or click another text field.
My goal is to input a number into a field and have the other fields update simultaneously. I tried clicking “Continuous” in the text field attributes but it doesn’t seem to do anything.
Here is my interface file:
#import <Foundation/Foundation.h>
@interface InchController : NSObject {
IBOutlet NSTextField *centimetersTextField;
IBOutlet NSTextField *inchesTextField;
IBOutlet NSTextField *feetTextField;
}
-(IBAction)convert:(id)sender;
@end
Here is my implementation file:
#import "InchController.h"
@implementation InchController
- (IBAction)convert:(id)sender {
if (sender == inchesTextField) {
float inches = [inchesTextField floatValue];
[feetTextField setFloatValue:(inches * 0.0833)];
[centimetersTextField setFloatValue:(inches * 2.54)];
}
else if (sender == feetTextField) {
float feet = [feetTextField floatValue];
[inchesTextField setFloatValue:(feet * 12)];
[centimetersTextField setFloatValue:(feet * 30.48)];
}
else if (sender == centimetersTextField) {
float centimeters = [centimetersTextField floatValue];
[inchesTextField setFloatValue:(centimeters * 0.394)];
[feetTextField setFloatValue:(centimeters * 0.033)];
}
}
@end
So here is the updated implementation file per Josh’s solution. Commented out the IBAction since it is no longer needed in the implementation and interface files.
#import "LengthController.h"
@implementation LengthController
//- (IBAction) convert: (id)sender {
//}
-(void) controlTextDidChange:(NSNotification *) note {
NSTextField *changedField = [note object];
if (changedField == inchesTextField) {
float inches = [inchesTextField floatValue];
[feetTextField setFloatValue: (inches * 0.0833)];
[centimetersTextField setFloatValue: (inches * 2.54)];
}
if (changedField == centimetersTextField) {
float centimeters = [centimetersTextField floatValue];
[inchesTextField setFloatValue:(centimeters * 0.394)];
[feetTextField setFloatValue:(centimeters * 0.033)];
}
if (changedField == feetTextField) {
float feet = [feetTextField floatValue];
[inchesTextField setFloatValue:(feet * 12)];
[centimetersTextField setFloatValue:(feet * 30.48)];
}
}
@end
Make your controller the delegate of the text fields; you can set this in Interface Builder by Ctrl-dragging from the text fields to the controller.
In your controller, implement the “
NSControlDelegate” methodcontrolTextDidChange:, which will be called (as its name suggests) whenever the field’s text changes. In that method, you can validate the text and, if appropriate, update the contents of the other fields.The argument that is passed in can give you the text field which changed; you can then pass that on to your existing
convert:method to reuse the code:There’s nothing special about action methods. The
IBActionreturn type evaluates tovoid; it’s only used by Xcode to expose the method for use in Interface Builder. You can, therefore, call them just like any other method. Here, you get the appropriate field and pass it in as thesenderparameter, as if the field had called the action method itself.