NSTextField的文本更改通知 [英] Text change notification for an NSTextField
问题描述
我想使用这个问题的答案中的代码:如何在NSTextField上观察NSTextField的值,以便观察存储在NSTextField中的字符串的变化。
I would like to use the code from the answer to this question: How to observe the value of an NSTextField on an NSTextField in order to observe changes on the string stored in the NSTextField.
[[NSNotificationCenter defaultCenter]
addObserverForName:NSTextViewDidChangeSelectionNotification
object:self.textView
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note){
NSLog(@"Text: %@", self.textView.textStorage.string);
}];
这里使用的类是一个NSTextView。我在NSTextField中找不到通知,而不是NSTextViewDidChangeSelectionNotification。
The class used here is an NSTextView. I can't find a notification in NSTextField to use instead of NSTextViewDidChangeSelectionNotification.
在NSTextField中是否有可用的通知?
Is there a notification available in NSTextField that can be used in this case ?
推荐答案
如果只想检测文本字段的值是否已更改,可以使用 controlTextDidChange: / code>
委托方法 NSTextField
继承自 NSControl
。
If you just want to detect when the value of a text field has changed, you can use the controlTextDidChange:
delegate method that NSTextField
inherits from NSControl
.
只需将nib文件中的 NSTextField
的委托
插口连接到控制器类,并实现这样:
Just connect the delegate
outlet of the NSTextField
in the nib file to your controller class, and implement something like this:
- (void)controlTextDidChange:(NSNotification *)notification {
NSTextField *textField = [notification object];
NSLog(@"controlTextDidChange: stringValue == %@", [textField stringValue]);
}
如果您创建 NSTextField
程序化,您可以使用 NSTextField
的 setDelegate:方法:
If you're creating the NSTextField
programmatically, you can use NSTextField
's setDelegate: method after creation to specify the delegate:
NSTextField *textField = [[[NSTextField alloc] initWithFrame:someRect] autorelease];
[textField setDelegate:self]; // or whatever object you want
委托是Cocoa中使用的基本设计模式之一。简而言之,它允许您轻松地定制标准对象(在这种情况下,用户界面对象)的行为,而不需要将对象子类化以添加附加行为。例如,检测文本字段中的文本何时更改的另一种较低级别的方法可能是创建您自己的自定义 NSTextField
子类,其中覆盖<$c$ c> keyDown: 方法 NSTextField
从继承NSResponder
。然而,这样的子类化很困难,因为它可能要求你有一个对象的继承层次结构的密切知识。有关详情,请查看以下内容:
Delegation is one of the fundamental design patterns used throughout Cocoa. Briefly, it allows you to easily customize the behavior of standard objects (in this case, user interface objects) without the complexity involved in having to subclass the object to add that additional behavior. For example, another lower-level way to detect when the text in a textfield has changed might be to create your own custom NSTextField
subclass in which you override the keyDown:
method that NSTextField
inherits from NSResponder
. However, subclassing like that is difficult because it can require that you have an intimate knowledge of the object's inheritance hierarchy. For more info, definitely check out the following:
关于 id< NSTextFieldDelegate>
意味着:它表示一个通用对象( id
),声明自己符合< NSTextFieldDelegate>
协议。有关协议的详情,请参阅 Objective-C编程语言:协议。
Regarding what id <NSTextFieldDelegate>
means: it means a generic object (id
) that declares itself as conforming to the <NSTextFieldDelegate>
protocol. For more info on protocols, see The Objective-C Programming Language: Protocols.
示例GitHub项目位于: https://github.com/NSGod/MDControlTextDidChange
Sample GitHub project at: https://github.com/NSGod/MDControlTextDidChange
这篇关于NSTextField的文本更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!