1

I've recently started working on some test projects to get the feel for OS X development with Xcode. I come from Windows, so I might not be making much sense here.

How would I subscribe to certain "events" in Swift? I have just learned how to connect actions to UI objects. For example, I can now click a button, and change the text of a label programatically. However, and this may just be a case of lack of knowledge on my part - I am not able to find a way to subscribe to a TextField's "Text Changed" event.

Let's say that I have a TextField, and when I change the text at runtime (i.e. type something), I want to do something in the textChanged event for that particular TextField.

Is there even such a thing as a TextChanged event in OS X development?

Update


I am now using the following code:

import Cocoa

class ViewController: NSViewController {

    class textField:NSTextField, NSTextFieldDelegate
    {
        override func awakeFromNib() {
            delegate = self;
        }

        override func controlTextDidChange(obj: NSNotification)
        {
            println("Text changed.")
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

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

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

And I have added a ClassName to the TextField control in the Identity Inspector, but it isn't responding to the text changing. The message given is:

Failed to connect (textField) outlet from (Xcode_Action_Basics.ViewController) to (NSTextField): missing setter or instance variable

I just googled that error and came across this page: Failed to connect (storyboard) outlet from (NSApplication) to (NSNibExternalObjectPlaceholder) error in Cocoa and storyboard which states that this is a known issue in Xcode and that it does not mean there is a problem with your code - but I'm not so sure about that, because the code isn't working. Not sure if I've missed out on something.

Community
  • 1
  • 1
RǢF
  • 806
  • 5
  • 17
  • possible dublicate: [link](http://stackoverflow.com/questions/7010547/uitextfield-text-change-event) – Sharky Feb 02 '15 at 11:24
  • Please, no more mixing iOS and OS X stuff. I've followed through countless tutorials in the past week which were advertised as OS X tutorials, only to realise that the code doesn't * work because it targets iOS. – RǢF Feb 02 '15 at 11:27
  • @Sharky You know that panel that lists the events/actions for the selected control in Xcode? Why doesn't it display all the actions/events for that selected control? Why only a couple? Is there a way to get it to show all of them? – RǢF Feb 02 '15 at 11:28
  • RE deleted question: See this [screenshot](http://i.stack.imgur.com/JUEeO.png), note the areas outlined in red. – zaph Feb 02 '15 at 15:15

1 Answers1

2

Create a class that implements the protocol NSTextFieldDelegate like

class MyTextField:NSTextField, NSTextFieldDelegate {
  override func awakeFromNib() {
    delegate = self // tell that we care for ourselfs
  }
  override func controlTextDidChange(obj: NSNotification) {
    //  .... handle change, there are a lot of other similar methods. See help
  }
}

In IB assign this class here:

enter image description here

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Thank you for the answer and code, I'm checking this out now. – RǢF Feb 02 '15 at 11:41
  • I have updated my question with the results. The code you posted didn't work - I added the code - added a class name in the ID inspector, built and ran the app and it still doesn't respond to text changing. – RǢF Feb 02 '15 at 11:59
  • 1
    The class must be on its own (an extra file). Else you can't assign it in IB. – qwerty_so Feb 02 '15 at 12:10
  • Ah, I just put it in a new class file, `textField.swift` and it's in the same directory as ViewController.swift and Main.storyboard. But it's still telling me the same thing. – RǢF Feb 02 '15 at 12:32
  • Hard to tell this way. You should name your class and file with firs upper char (as a side note). Who tells what? I see you use an outlet with the same name. So GOOD advise: Types upper case first, instances lower case. – qwerty_so Feb 02 '15 at 12:52
  • Thanks, I changed to uppercase but no change. I then (by fluke) decided to set `Module` to ProjectName in the dropdown and rebuilt and it worked. It now detects text changed event and responds correctly. However I still am getting the `missing setter or instance variable` message in the output window. – RǢF Feb 02 '15 at 13:07
  • It's from an old outline without reference. Open File's Owner in IB and there Connections Inspector. Delete those with an exclamation mark. – qwerty_so Feb 02 '15 at 13:10