Press "Enter" to skip to content

About

This is the latest incarnation of my weblog.  Feel free to poke around.

7 Comments

  1. David Lohnes
    David Lohnes February 10, 2012

    Are you, by chance, the Russell Finn that ported Pirates! to the Macintosh in the late 80s? If, so, I have some questions for you.

  2. David Lohnes
    David Lohnes February 10, 2012

    Ah, you ARE! A well-built LinkedIn profile for the win!

    I am a part of an informal group of Mac preservationists dedicated to recording the history of Mac software in the 68k days. I was hoping you’d be willing to answer some questions about the projects you worked on for MicroProse. If you are, shoot me an e-mail at arthegall@gmail.com. Thanks.

    David

  3. Russell Finn
    Russell Finn February 10, 2012

    Hi, yes, that’s me. Gee, that was a long time ago… I’ll shoot you an email this weekend.

  4. Andrew
    Andrew September 4, 2017

    Hi Russell

    I stumbled across your blog while researching what I thought would be a simple thing but can’t find a solution. As a self taught programmer in Swift I have written a couple of iOS apps and learnt a lot but struggling with my first macOS app which includes a NSTextField. All I want to do is when user clicks inside the text field an action func runs. I have tried NSTextField and NSTextFieldcell but don’t understand how it should work, any poiters would be useful.

    • Russell Finn
      Russell Finn September 4, 2017

      Hi, Andrew —

      The answer to your question depends somewhat on whether your text field is editable or not.

      If the text field is not editable, it’s pretty straightforward — since NSTextField inherits from NSControl, it has a mouseDown(_:) method. By default a non-editable NSTextField does not respond to mouse clicks, but it’s easy to write a subclass that does:

      class ClickableTextField: NSTextField {
          override func mouseDown(with event: NSEvent) {
              self.sendAction(self.action, to: self.delegate)
          }
      }

      (I’ve used self explicitly here to call out the method and properties that are already present on an NSControl.)

      Once you have this class in your code, you can create an NSTextField in Interface Builder, change its class to ClickableTextField in the Identity Inspector, and then set up a class to act as the delegate of the NSTextField and implement an action method. Typically this would be a controller class already in your app (perhaps the window controller for the window that contains your text field). Then assign the delegate and action properties of the text field in Interface Builder, and you’re done.

      If the text field is intended to be editable, then things get more complicated. In this case, the NSTextField already responds to a mouse down event by setting up the field editor — a single NSTextView shared among all the text fields in a window. Once the text field is active, all the mouse clicks go to the NSTextView as part of the editing session. If it’s actually your intention to monitor these mouse clicks, then you probably have a lot more work to do (for a sense of how much, see the Apple documentation for the Cocoa text architecture, specially the section titled “Working with the Field Editor”). I would suggest considering what your actual goal is; if you find yourself fighting with AppKit, there’s usually a different/better way to accomplish your goal.

      Hope this helps. Thanks for dropping by.

  5. Andrew
    Andrew September 10, 2017

    Hi Russell

    Just found your detailed reply so thanks very much for that. I do need the textfield to be editable and have read thru the Apple documents you linked to but got hopelessly lost. I can populate the text field by double clicking on a table cell or just by clicking in the text field itself but do a question. I have for testing purposes created 2 text fields, one NSTextfield and a NSTextfieldcell but don’t know what the differences are. Ideally I would like to know when user clicks in textfield AND update the character counter as they type I am using:- imputTextFieldCell.accessibilityNumberOfCharacters(). Can you please suggest a tutorial or example that might help me as I can find very little for macOS apps?

    Andrew

    • Russell Finn
      Russell Finn October 3, 2017

      Andrew —

      Let me try to point you to some resources that may help you.

      • You mostly work with NSTextFields directly; NSTextFieldCell is an implementation detail used by AppKit to implement the NSTextField. See the Apple documentation “Text Fields, Text Views, and the Field Editor”, and this answer on Stack Overflow.
      • For tutorials, you can try Ray Wenderlich’s site, perhaps this “macOS Controls Tutorial” that includes a discussion of NSTextFields. After that, you may consider going on to “Cocoa Bindings on macOS”. Cocoa Bindings are a complex and advanced feature that may nevertheless turn out to be a good way to get to your intended functionality. Specifially, accessibilityNumberOfCharacters is probably not the best way to count the characters in the text field; instead, you can “bind” a variable in your application (usually a property in your window or view controller) to the expression “inputTextField.string.length”, and the variable will then be updated for you whenever the string is edited. You can then bind the string value of another text field to that variable, and it will display the character count. (This may make more sense after you read the linked article.)

      I hope this helps. Good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *