How to live check a NSTextField - Swift OS X
- Conforms
ViewController
to protocolNSTextDelegate
. - Assign
ViewController
as Delegate forTextField
. Implement
controlTextDidChange
method.import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var textField: NSTextField! @IBOutlet weak var label: NSTextField! func applicationDidFinishLaunching(aNotification: NSNotification) { textField.delegate = self } override func controlTextDidChange(notification: NSNotification) { let object = notification.object as! NSTextField self.label.stringValue = object.stringValue } func applicationWillTerminate(aNotification: NSNotification) { // Insert code here to tear down your application } }
in ViewController:
import Cocoa
class ViewController: NSViewController, NSTextDelegate {
@IBOutlet weak var label: NSTextField!
@IBOutlet weak var label2: NSTextField!
@IBOutlet weak var textField: NSTextField!
@IBOutlet weak var textField2: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func controlTextDidChange(notification: NSNotification) {
if let txtFld = notification.object as? NSTextField {
switch txtFld.tag {
case 201:
self.label.stringValue = txtFld.stringValue
case 202:
self.label2.stringValue = txtFld.stringValue
default:
break
}
}
}
}
swift 5 xcode 10
//TODO: move oin a better place?
extension NSTextField{ func controlTextDidChange(obj: NSNotification){} }
class TracksController:NSViewController,
NSTextFieldDelegate
{
...
}
override func viewDidLoad() {
super.viewDidLoad()
//Search bar:
self.searchBar.delegate = self
NotificationCenter.default.addObserver(self,
selector: #selector(controlTextDidChange(_:)),
name: NSTextView.didChangeSelectionNotification,
object: nil)
}
//MARK: NSTextFieldDelegate
func controlTextDidChange(_ obj: Notification)
{
guard let object = obj.object as? NSTextView else{
return
}
let delegate = object.delegate
guard let searchField = delegate as? NSSearchField else {
return
}
let text = searchField.stringValue
print(text)
}
I know it’s been answered some while ago but I did eventually find this solution for macOS in Swift 3 (it doesn’t work for Swift 4 unfortunately) which notifies when a textfield is clicked inside and for each key stroke.
Add this delegate to your class:-
NSTextFieldDelegate
In viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: Notification.Name.NSTextViewDidChangeSelection, object: nil)
Then add this function:-
func textDidChange(_ notification: Notification) {
print("Its come here textDidChange")
guard (notification.object as? NSTextView) != nil else { return }
let numberOfCharatersInTextfield: Int = textFieldCell.accessibilityNumberOfCharacters()
print("numberOfCharatersInTextfield = \(numberOfCharatersInTextfield)")
}
Hope this helps others.