56

我想使用这个问题的答案中的代码: 如何观察 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 中的通知吗?

4

5 回答 5

113

如果您只想检测文本字段的值何时发生变化,您可以使用controlTextDidChange:继承NSTextFieldNSControl.

只需将 nib 文件中的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覆盖继承自. 但是,这样的子类化很困难,因为它可能要求您对对象的继承层次有深入的了解。有关更多信息,请务必查看以下内容:keyDown:NSTextFieldNSResponder

Cocoa 基础指南:代表和数据源

关于什么id <NSTextFieldDelegate>意思:它意味着一个通用对象(id),它声明自己符合<NSTextFieldDelegate>协议。有关协议的更多信息,请参阅 Objective-C 编程语言:协议

示例 GitHub 项目位于:https ://github.com/NSGod/MDControlTextDidChange

于 2012-07-15T01:09:29.603 回答
10

Xcode 9.2。使用 Swift 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
于 2018-03-05T17:08:07.513 回答
7

您应该使用NSTextFieldDelegate和实施controlTextDidChange. 在 macOS 10.14 和 Swift 4.2 中测试

import Cocoa

class ViewController: NSViewController, NSTextFieldDelegate {

  @IBOutlet weak var textField: NSTextField!

  override func viewDidLoad() {
    super.viewDidLoad()

    textField.delegate = self
  }

  func controlTextDidChange(_ obj: Notification) {
    let textField = obj.object as! NSTextField
    print(textField.stringValue)
  }
}
于 2019-02-20T13:55:12.597 回答
0

我相信您想阅读字段编辑器,它本质上是一个(隐藏的) ,它处理给定窗口NSTextView中所有 s 的文本输入。“使用字段编辑器使用委派和通知”NSTextField部分应该为您指明正确的方向。

于 2012-07-14T22:36:37.000 回答
0

在 Swift 中是

public override func controlTextDidChange(_ obj: Notification) {


}
于 2017-09-02T09:51:56.647 回答