我尝试使用NSTextField
的更改用户输入动态更新其他视图的文本。以下方法不适用,因为它适用于iOS
。是否有类似osx
NSTextField
可用的内容?
self.equationTextField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
//build error: "Use of unresolved identifier 'UIControlEvents'"
谢谢!
答案 0 :(得分:10)
您必须使用NSControlTextEditingDelegate。如果您使用的是故事板,请将NSTextField delegate
连接到ViewController。如果您以编程方式创建它,只需将其设置为如下代码:textfield.delegate = self
中的viewDidLoad()
在NSControlTextEditingDelegate
中,您可以使用controlTextDidChange
在编辑文本时获取通知。
extension ViewController: NSControlTextEditingDelegate {
override func controlTextDidChange(_ notification: Notification) {
if let textField = notification.object as? NSTextField {
print(textField.stringValue)
//do what you need here
}
}
}
答案 1 :(得分:2)
NSControlTextEditingDelegate
controlTextDidChange和controlTextDidEndEditing位于 NSControlTextEditingDelegate
class MyViewController:.... NSControlTextEditingDelegate
textfield.delegate = self
//compile error: Cannot assign value of type 'MyViewController' to type 'NSTextFieldDelegate?'
//
class MyViewController:.... NSTextFieldDelegate
textfield.delegate = self //compiles ok
//
// MyViewController.swift
//
import Cocoa
class MyViewController: NSViewController, NSTextFieldDelegate {
//--------------------------------------------------------------
// MARK: - //call in viewDidLoad
// MARK: -
//--------------------------------------------------------------
func connectTONSControlTextEditingDelegate(){
//controlTextDidChange and controlTextDidEndEditing are in NSControlTextEditingDelegate
//this is a parent protocol of NSTextFieldDelegate
//protocol NSTextFieldDelegate : NSControlTextEditingDelegate
//so implement NSTextFieldDelegate
//class MyViewController:.... NSTextFieldDelegate
//then connect it here to NSTextFields
//then override controlTextDidChange OR controlTextDidEndEditing
self.textField0.delegate = self
self.textField1.delegate = self
}
//--------------------------------------------------------------
// MARK: - NSControlTextEditingDelegate
// MARK: -
//--------------------------------------------------------------
//called everytime you type in field
override func controlTextDidChange(_ notification: Notification) {
if let textField = notification.object as? NSTextField {
if textField === textField0{
logger.info("[controlTextDidChange][textField0]:'\(textField.stringValue)'")
}
else if textField === textField1{
logger.info("[controlTextDidChange][textField1]:'\(textField.stringValue)'")
}
else{
logger.error("[controlTextDidChange][UNHANDLED NSTextField]")
}
}
}
//User tapped in NSTextfields and then clicked out - text may not have changed
//this is actually on NSObject in appkit
//dont need both controlTextDidChange and controlTextDidEndEditing but no harm if you do
override func controlTextDidEndEditing(_ notification: Notification){
if let textField = notification.object as? NSTextField {
if textField === textField0{
logger.info("[controlTextDidEndEditing][textField0]:'\(textField.stringValue)'")
}
else if textField === textField1{
logger.info("[controlTextDidEndEditing][textField1]:'\(textField.stringValue)'")
}
else{
logger.error("[controlTextDidEndEditing][UNHANDLED NSTextField]")
}
}
}
//--------------------------------------------------------------
// MARK: - NSTextFields
// MARK: -
//--------------------------------------------------------------
@IBOutlet weak var textField0: NSTextField!
@IBOutlet weak var textField1: NSTextField!
//--------------------------------------------------------------
// MARK: - viewDidLoad
// MARK: -
//--------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
//---------------------------------------------------------
//setup text change delegates - see extension above
connectTONSControlTextEditingDelegate()
//---------------------------------------------------------
}
}