Problem avoiding first responder on a NSTextField


Problem avoiding first responder on a NSTextField



I need to change behavior of input fields in a really simple app:
Simple OSX app



Whenever i launch the application the first text field get the focus, but i don't want this behavior.



I tried checking "Refuses first responder" in IB. It works but with this option checked i can't move between input fields pressing "tab" button.



What can i do to avoid focus at startup and keep the ability to move with tab keyboard button ?





Not really an answer since i didn't tried it on MacOS, but on iOS I would use [textfield resignFirstResponder] on load.
– Valentin Radu
Aug 11 '11 at 11:02





@mindnoise nothing to do, it still maintains focus.
– MatterGoal
Aug 11 '11 at 12:17




4 Answers
4



The (previously) accepted answer isn't reliable and doesn't work very well. The other answer with the hidden NSTextField isn't very great either because now you have a new element in your Tab order.


NSTextField



The solution I've found works best so far is:



Make the NSTextField refusesFirstResponder YES on app launch.


NSTextField refusesFirstResponder


YES



Then, in viewDidAppear for the controller, go ahead and set refusesFirstResponder back to NO.


viewDidAppear


refusesFirstResponder


NO



Everything behaves perfect after launch, and I don't have a greedy NSTextField stealing first responder on app startup.


NSTextField



I found the solution, you can add [window makeFirstResponder:nil]; after awakeFromNib for example in applicationDidfinishLaunching.


[window makeFirstResponder:nil];


applicationDidfinishLaunching



window?.makeFirstResponder(nil) does not work for me - when I check who is the first responder, it is the window (and not a NSTextField) but still, the first NSTextField is selected and active. The solution for me (though I know not the cleanest one) was to create a hidden text field and make it the first responder every time the window did load.


window?.makeFirstResponder(nil)



window?.makeFirstResponder(nil) worked only when I set all NSTextFields to RefuseFirstResponder, but then using Tab to switch between them of course do not work.


window?.makeFirstResponder(nil)


RefuseFirstResponder



This worked for me,


override func viewDidAppear() {

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { [weak self] (timer) in
NSApplication.shared.windows[0].makeFirstResponder(self?.textUsername)
let tRange = self?.textUsername.currentEditor()?.selectedRange
self?.textUsername.currentEditor()?.selectedRange = NSMakeRange((tRange?.length)!, 0)
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux