如何正确使用NSTextField的委托

时间:2022-09-06 19:25:20

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.

我试图为NSTextField对象实现一个委托,这样我就可以实时检测用户输入,并给出一些关于该特定字段中没有允许输入的反馈。特别是,我想从JavaScript模拟o​​nChange()方法,实时检测用户输入并在编写不受支持的值时向他显示警告。

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.

即应用程序有一个文本字段,它只接受0到255之间的数值(如RGB值),我想知道用户何时写入不是数值或超出范围值,以立即显示警告消息或更改文本场背景色,只是一个视觉提示,让他知道输入它是错的。

如何正确使用NSTextField的委托如何正确使用NSTextField的委托

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.

我一直在阅读很多Apple的文档,但我不明白要实现哪个委托(NSTextFieldDelegate,NSTextDelegate或NSTextViewDelegate),我也不知道如何在我的AppDelegate.m文件中实现它以及使用哪个方法和如何获取用户编辑通知。

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.

现在,我已经在我的init方法中使用类似的东西设置了委托[self.textField setDelegate:self];但我不明白如何使用它或实现哪种方法。

2 个解决方案

#1


5  

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

首先,我必须在AppDelegate.h文件中声明NSTextFieldDelegate

@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.

之后,当用户在AppDelegate.m文件中更新它时,我必须实例化我想要修改的NSTextField对象的委托。

- (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];
    }
}

#2


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.

使您的类符合NSTextFieldDelegate协议。它需要成为该协议,因为在文档中它表示委托符合的协议类型。

@interface MyClass : NSObject

@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

我认为在你的情况下,最好将TextField替换为TextView并使用NSTextViewDelegate,在委托中,你最多的方法应该是

- (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
}

#1


5  

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

首先,我必须在AppDelegate.h文件中声明NSTextFieldDelegate

@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.

之后,当用户在AppDelegate.m文件中更新它时,我必须实例化我想要修改的NSTextField对象的委托。

- (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];
    }
}

#2


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.

使您的类符合NSTextFieldDelegate协议。它需要成为该协议,因为在文档中它表示委托符合的协议类型。

@interface MyClass : NSObject

@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

我认为在你的情况下,最好将TextField替换为TextView并使用NSTextViewDelegate,在委托中,你最多的方法应该是

- (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
}