ViewController + Storyboard setting up validation with controlTextDidChange

306 views Asked by At

Trying to setup validation for a few text fields in a new (and very small) Swift Mac app. Following various other topics here on SO and a few other examples, I can still not get controlTextDidChange to propagate (to my ViewController).

E.g: How to live check a NSTextField - Swift OS X

I have read at least a dozen variations of basically that same concept. Since none of the accepted answers seem to work I am just getting more and more confused by something which is generally a fairly simple task on most platforms.

I have controlTextDidChange implemented to just call NSLog to let me know if I get anything.

AppDelegate should be part of the responder chain and should eventually handle controlTextDidChange but I see nothing there either.

Using the current Xcode I start a new project. Cocoa app, Swift, Storyboard and nothing else.

From what I can gather the below isolated example should work. In my actual app I have tried some ways of inserting the ViewController into the responder chain. Some answers I found suggested it was not always there. I also tried manually adding the ViewController as the delegate in code theTextField.delegate = self

Nothing I have done seems to get text changed to trigger any events.

Any ideas why I have so much trouble setting up this delegation?

My single textfield example app

Storyboard is about as simple as it gets:

Storyboard with single NSTextField

NSTextField Connections

AppDelegate

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate, NSTextDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func controlTextDidChange(notification: NSNotification) {
        let object = notification.object as! NSTextField
        NSLog("AppDelegate::controlTextDidChange")
        NSLog("field contains: \(object.stringValue)")
    }
}

ViewController

import Cocoa

class ViewController: NSViewController, NSTextFieldDelegate, NSTextDelegate {

    @IBOutlet var theTextField: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    func controlTextDidChange(notification: NSNotification) {
        let object = notification.object as! NSTextField
        NSLog("ViewController::controlTextDidChange")
        NSLog("field contains: \(object.stringValue)")
    }
}
1

There are 1 answers

2
Phillip Mills On BEST ANSWER

I think the samples you're following are a bit out-of-date. Try...

    override func controlTextDidChange(_ notification: Notification) {

...as the function definition for your method in your NSTextFieldDelegate.