2012-07-14 85 views
42

我想使用此问题的答案中的代码:How to observe the value of an NSTextField在NSTextField上,以便观察存储在NSTextField中的字符串的更改。NSTextField的文本更改通知

[[NSNotificationCenter defaultCenter] 
    addObserverForName:NSTextViewDidChangeSelectionNotification 
    object:self.textView 
    queue:[NSOperationQueue mainQueue] 
    usingBlock:^(NSNotification *note){ 
    NSLog(@"Text: %@", self.textView.textStorage.string); 
}]; 

这里使用的类是一个NSTextView。我在NSTextField中找不到NSTextViewDidChangeSelectionNotification的通知。

在这种情况下,NSTextField中是否有可用的通知?

+1

你能澄清是否你正在寻找时通知文本字段的stringValue的变化,或当所选文本的变化(如链接的答案交易)? – NSGod 2012-07-15 01:11:54

回答

89

如果您只是想检测文本字段的值何时发生更改,则可以使用NSTextFieldNSControl继承的controlTextDidChange:委托方法。

只需连接delegate出口NSTextField的笔尖文件到您的控制器类,并实现这样的事情:

- (void)controlTextDidChange:(NSNotification *)notification { 
    NSTextField *textField = [notification object]; 
    NSLog(@"controlTextDidChange: stringValue == %@", [textField stringValue]); 
} 

如果您正在创建的NSTextField编程,您可以使用NSTextFieldsetDelegate:创建后指定代理的方法:

NSTextField *textField = [[[NSTextField alloc] initWithFrame:someRect] autorelease]; 
[textField setDelegate:self]; // or whatever object you want 

委派是整个Cocoa使用的基本设计模式之一。简而言之,它允许您轻松定制标准对象(在这种情况下为用户界面对象)的行为,而无需为子对象添加额外的行为而带来复杂性。例如,检测文本字段中的文本何时发生更改的另一种较低级别的方法可能是创建您自己的自定义NSTextField子类,在此子类中,您覆盖继承自NSResponderkeyDown:方法。但是,像这样的子类很困难,因为它可能要求您对对象的继承层次有深入的了解。欲了解更多信息,肯定检查了以下工作:

Cocoa Fundamentals Guide: Delegates and Data Sources

关于什么id <NSTextFieldDelegate>意味着:它意味着一个通用对象(id)声明本身符合<NSTextFieldDelegate>协议。有关协议的更多信息,请参阅The Objective-C Programming Language: Protocols

样品GitHub的项目在:https://github.com/NSGod/MDControlTextDidChange

+0

我不是用IB创建NSTextField,而是以编程方式。有一个等价的方法? – alecail 2012-07-15 02:23:47

+0

已更新的程序创建答案 – NSGod 2012-07-15 10:32:25

+0

代表对我来说是新的。我不知道'id 是什么,以及在哪里放置'setDelegate'的'controlTextDidChange'方法但我很快就会发现 – alecail 2012-07-15 10:55:01

0

我相信你想要读取field editor,它本质上是一个(隐藏)NSTextView,它处理给定窗口中所有NSTextField的文本输入。关于"Using Delegation and Notification With the Field Editor"的部分应该指向正确的方向。

+1

破碎的链接:(试试这里:https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextFieldsAndViews/TextFieldsAndViews.html – ericpeters0n 2014-06-18 22:19:35

0

在斯威夫特是

public override func controlTextDidChange(_ obj: Notification) { 


} 
0

的Xcode 9.2。与斯威夫特4.0.3。

必须通过接口生成器连接NSTextField才能使此实现正常工作。

import Cocoa 

@objc public class MyWindowController: NSWindowController, NSTextFieldDelegate { 

@IBOutlet weak var myTextField: NSTextField! 

// MARK: - ViewController lifecycle - 

override public func windowDidLoad() { 
    super.windowDidLoad() 
    myTextField.delegate = self 
} 

// MARK: - NSTextFieldDelegate - 

public override func controlTextDidChange(_ obj: Notification) { 
    // check the identifier to be sure you have the correct textfield if more are used 
    if let textField = obj.object as? NSTextField, self.myTextField.identifier == textField.identifier { 
     print("\n\nMy own textField = \(self.myTextField)\nNotification textfield = \(textField)") 
     print("\nChanged text = \(textField.stringValue)\n") 
    } 
} 

}

控制台输出:

My own textField = Optional(<myApp.NSTextField 0x103f1e720>) 
Notification textfield = <myApp.NSTextField: 0x103f1e720> 

Changed text = asdasdasddsada