On May 31, 2017, at 1:51 PM, J.E. Schotsman <[email protected]> wrote:
> 
> I have a hard time achieving what the message title says.
> Depending on user settings I want a text field to be either editable or 
> non-editable.
> 
> I’ve tried this:
> 
> if makeEditable
>       {               myTextField.isEditable = false
>                       myTextField.drawsBackground = false }
> else
>       {               myTextField.isEditable = true
>                       myTextField.drawsBackground = true }
> 
> Setting the editable property has the desired effect, but setting 
> drawsBackground has no effect.
> Also I need to move the focus to another control (if myTextField has focus) 
> or else the insertion point indicator keeps blinking.
> I’ve tried this:
> 
>                       if  myTextField.window!.firstResponder === myTextField
>                               {
>                               myTextField.window!.selectPreviousKeyView(nil)
>                               }
> 
> but no cigar.

Well, the window's first responder will never be the text field.  When a text 
field has focus, it's not actually the text field that is first responder.  A 
special text _view_, known as the field editor, is added to the window's view 
hierarchy in front of the text field and it is the actual first responder and 
handles the editing.  If you need to know if a given text field has the focus, 
you need to use code similar to that shown in Listing 3-8 under Determining 
First-Responder Status[1] in the Cocoa Event Handling Guide: Event Handling 
Basics.

If you want to end editing, you should use code similar to that shown in the 
docs for the endEditing(for:) method[2] of NSWindow.  You can try using 
selectPreviousKeyView() instead of the call to makeFirstResponder(_:) in those 
docs, but the problem is that it doesn't return whether it was successful or 
not.  So, you would try it and then check the first responder again.

[1] 
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/EventOverview/EventHandlingBasics/EventHandlingBasics.html#//apple_ref/doc/uid/10000060i-CH5-SW23
[2] https://developer.apple.com/reference/appkit/nswindow/1419469-endediting

> firstResponder always is some button even when I see the insertion point 
> indicator blinking.

That seems unlikely.

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to