I cannot figure out how to automatically update NSTextfield
without pressing "Return" or clicking on another text field.
My goal is to enter a number in the field and update other fields at the same time. I tried to click "Continuous" in the attributes of the text field, but it does nothing.
Here is my interface file:
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 for the Josh solution. Commented on 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
source share