Question

Im trying to implement a delegate for a NSTextField object so I can detect the user input in real time and give some feedback about no allowed input in that particular field. Especially, I want to simulate the onChange() method from JavaScript, detecting the user input in real time and show him a warning if it is writing a non supported value.

i.e. The app have a text field it only accept numeric values from 0 to 255 (like RGB values) and I want to know when the user is writing not numeric values or out of range values to instantly show him a warning message or change the text field background color, just a visual hint to let him know the input it's wrong.

enter image description here enter image description here

Like you see on the pictures above, I want to show a warning sign every time the user inputs a forbidden value in the text field.

I have been reading a lot of the Apple's documentation but I don't understand which delegate to implement (NSTextFieldDelegate, NSTextDelegate, or NSTextViewDelegate), also, I have no idea how to implement it in my AppDelegate.m file and which method use and how to get the notification of user editing.

Right now, I already set the Delegate in my init method with something like this [self.textField setDelegate:self]; but I don't understand how to use it or which method implements.

Was it helpful?

Solution

I found a solution using the information posted in this question... Listen to a value change of my text field

First of all I have to declare the NSTextFieldDelegate in the AppDelegate.h file

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTextFieldDelegate>

After that, I have to instantiate the delegate for the NSTextField object I want to modify while the user update it in the AppDelegate.m file.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self.textField setDelegate:self];
}

Finally, I implement the methods to detect field editing with the changes I want to set.

- (void)controlTextDidChange:(NSNotification *)notification {
    NSTextField *textField = [notification object];
    if ([textField doubleValue] < 0 | [textField doubleValue] > 255) {
        textField.textColor = [NSColor redColor];
    }
}

- (void)controlTextDidEndEditing:(NSNotification *)notification {
    NSTextField *textField = [notification object];
    if ([textField resignFirstResponder]) {
        textField.textColor = [NSColor blackColor];
    }
}

OTHER TIPS

Make your class conform to the NSTextFieldDelegate protocol. It need's to be that protocol because in the documentation it says the type of protocol the delegate conforms to.

@interface MyClass : NSObject

And implement the delegate's methods (just add them to your code). Example

- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
{
}

EDIT:

I think in your case it would be better to replace the TextField for a TextView and use a NSTextViewDelegate, in the delegate, the method of most interst of you should be

- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
    BOOL isValid = ... // Check here if replacementString is valid (only digits, ...)
    return isValid; // If you return false, the user edition is cancelled
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top