Maintaining NSTextField Constantly

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:

 #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 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 
+4
source share
2 answers

Make your controller a delegate of text fields; You can set this in Interface Builder by dragging Ctrl from text fields to the controller.

In your controller, implement the " NSControl Delegate" method controlTextDidChange: which will be called (as the name suggests) whenever the field text changes. In this method, you can check the text and, if necessary, update the contents of other fields.

The argument that is passed can give you a text field that has changed; you can pass this to the existing convert: method to reuse the code:

 - (void) controlTextDidChange: (NSNotification *)note { NSTextField * changedField = [note object]; [self convert:changedField]; } 

There is nothing special about action methods. The returned IBAction type evaluates to void ; it is used only by Xcode to expose the method for use in Interface Builder. Therefore, you can call them just like any other method. Here you get the corresponding field and pass it as the sender parameter, as if the field called the action method itself.

+7
source

Depending on complexity, binding problems can also be a viable solution.

You can define the properties of the model object or controller model and connect them to the corresponding text fields. Then the changes in the text box are immediately reflected in the properties, which can then initiate changes to other properties.

Text fields associated with these "derived" properties are then automatically updated.

Remember to “copy” your changes to derived properties using willChangeValueForKey: and didChangeValueForKey: so that the changes are sent to observers. More details here .

Of course, if you have loops in dependencies, it gets ugly; in this case, the controlTextDidChange: method mentioned in other answers is probably better.

0
source

Source: https://habr.com/ru/post/1385052/


All Articles