nstextfieldfirst-responderresignfirstresponder

Mac NSTextField won't resign firstResponder


I have a window with some NSTextFields. When I click in one and edit the value and press return, I want the focus to go back to what it was before. I don't want the blue ring around the text field and I don't want further keystrokes going to that text field. I would have thought this would happen automatically.

I tried these, and none of them work

 sender.resignFirstResponder()

 sender.window?.makeFirstResponder(nil)

 InspectorWindowController.window?.makeFirstResponder(nil)

 AnotherWindowController.window?.becomeFirstResponder()

I'm doing these at the end of my IBAction associated with the text field. Maybe I have to do it from somewhere else?

Thanks


Solution

  • I figured this out. I guess the sent action is happening on another thread. So you have to call makeFirstResponder using Dispatch async.

    DispatchQueue.main.async { //omg
        sender.window?.makeFirstResponder(nil)
    }