Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa NSTextField的文本更改通知_Cocoa_Observer Pattern_Nstextfield_Nsnotifications - Fatal编程技术网

Cocoa NSTextField的文本更改通知

Cocoa NSTextField的文本更改通知,cocoa,observer-pattern,nstextfield,nsnotifications,Cocoa,Observer Pattern,Nstextfield,Nsnotifications,我想使用这个问题答案中的代码:在NSTextField上,以便观察存储在NSTextField中的字符串的更改 [[NSNotificationCenter defaultCenter] addObserverForName:NSTextViewDidChangeSelectionNotification object:self.textView queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotific

我想使用这个问题答案中的代码:在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中是否有可用于本例的通知?

我相信您希望了解,它本质上是一个(隐藏的)
NSTextView
,用于处理给定窗口中所有
NSTextField
的文本输入。上的部分应该为您指出正确的方向。

如果您只想检测文本字段的值何时发生更改,可以使用
NSTextField
NSControl
继承的委托方法

只需将nib文件中
NSTextField
delegate
出口连接到控制器类,并实现如下操作:

- (void)controlTextDidChange:(NSNotification *)notification {
    NSTextField *textField = [notification object];
    NSLog(@"controlTextDidChange: stringValue == %@", [textField stringValue]);
}
如果以编程方式创建
NSTextField
,则可以在创建后使用
NSTextField
的方法指定委托:

NSTextField *textField = [[[NSTextField alloc] initWithFrame:someRect] autorelease];
[textField setDelegate:self]; // or whatever object you want
委托是整个Cocoa中使用的基本设计模式之一。简单地说,它允许您轻松地自定义标准对象(在本例中为用户界面对象)的行为,而无需对对象进行子类化以添加其他行为。例如,检测textfield中的文本何时更改的另一个较低级别的方法可能是创建自己的自定义
NSTextField
子类,在该子类中重写
NSTextField
NSResponder
继承的方法。然而,像这样的子类化是很困难的,因为它需要您对对象的继承层次结构有深入的了解。有关更多信息,请务必查看以下内容:

关于
id
的含义:它是指声明自己符合
协议的通用对象(
id
)。有关协议的更多信息,请参阅

示例GitHub项目位于:Swift中的

public override func controlTextDidChange(_ obj: Notification) {


}

代码9.2。使用Swift 4.0.3

必须通过interface builder连接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
My own textField=可选()
通知文本字段=
已更改文本=asdasdsada

您应该使用
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)
  }
}

您能否澄清一下,您是希望在textfield的stringValue更改时收到通知,还是在所选文本更改时收到通知(如链接答案所述)?我不是使用IB创建NSTextField,而是以编程方式创建的。有没有一种等效的方法?我对学员不熟悉。我不知道
id
是什么,也不知道
controltextdichange
方法
setDelegate
要找的
id方法放在哪里,但我很快就会发现。对我来说不起作用,但textdicchange很好用!断开的链接:(请尝试此处: