Search code examples
swiftmacoscocoatextfieldfirst-responder

How to check which TextField is FirstResponder from another ViewController?


I'm using Swift 4 on macOS. I know that I can set the becomeFirstResponder() to a TextField like this:

mytextfield.becomeFirstResponder()

But how can I check if mytextfield is FirstResponder?


Solution

  • Your assumption is wrong about -becomeFirstResponder. From the documentation:

    Notifies the receiver that it’s about to become first responder in its NSWindow.

    You make something first responder through NSWindow's -makeFirstResponder:.

    To answer your question, the property is on NSWindow as well called firstResponder.