I’ve got a problem with a NSTextField inside a window. It appears to be disabled when I run the application and I can’t enter any text. It’s selectable, editable, uses single line mode, undo and draws background. It has a placeholder too. The window it’s in is being shown with…
my enterWindow's makeKeyAndOrderFront_(me)
tell animator() to setAlphaValue_(1)
It appears to be disabled when I run the application and I can’t enter any text. Why? How can I sort this?
Model: MacBook Pro
Operating System: Mac OS X (10.6)
First, a style point: using integers instead of enums saves typing, but it can be a pain when someone else looks at your code – we have to look them up. And you will too in six months.
What you’re seeing is “as designed”. The documentation for NSBorderlessWindowMask says “Useful only for display or caching purposes.” For better or worse, this is Apple’s way of encouraging UI uniformity.
That said, it can be done by subclassing NSWindow and overriding canBecomeKeyWindow so it returns yes. But you may still see some odd behavior, especially if you have other windows showing.
I didn’t know that, thanks! I changed it to a normal window and it works fine now.
I know I can use
textField's setStringValue_("Default value.")
to reset the title of a text field, but how do I set the placeholder?
I also tried
textField's setPlaceholderString_("Default value.")
You have to send that method to the text field’s cell: (textField’s |cell|()'s setPlaceholderString_(“Default value.”).
You won’t see the placeholder unless you set the textField’s string to an empty string (“”). You also won’t see it until the text field is no longer the current responder (by clicking in another responder or tabbing to it if you have set NextKeyView for the text field).
Have you found that empirically? I ask because the docs say:
“All of the methods declared by this class [NSTextFieldCell] are also declared by the NSTextField class, which uses NSTextFieldCell objects to draw and edit text. These NSTextField cover methods call the corresponding NSTextFieldCell methods.”
Yeah, I saw that too, but I did test it, and you get an unrecognized selector error message if you send it to the text field. I wonder if that was an oversight.