1

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.

David Gomez
  • 2,762
  • 2
  • 18
  • 28
  • I would recommend you to use an `NSNumberFormatter` for those kind of things. There is even an `NSTextField` with a number formatter in the sidebar of interface builder. – HAS Feb 20 '14 at 16:57
  • 1
    thank you for your recommendation, but I already have the formatter working fine, but I need to implement a visual warning just for user friendliness matters. – David Gomez Feb 20 '14 at 16:59

2 Answers2

10

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];
    }
}
Community
  • 1
  • 1
David Gomez
  • 2,762
  • 2
  • 18
  • 28
1

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
}
Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • Can you be a little more specific about how should I use this `textShouldBeginEditing` method because that's what I'm trying to use, but I don't understand how. I'm still too new in Cocoa Development (just like 2 weeks so far). – David Gomez Feb 20 '14 at 16:37
  • I can't tell you exactly what to do, what are you trying to accomplish? – Merlevede Feb 20 '14 at 16:44
  • I already updated my question with some pictures explaining my idea. – David Gomez Feb 20 '14 at 17:00