简体   繁体   中英

NSTextField is first responder but I still must click inside the text box before typing

My ultimate goal is to have an NSTextField selected by default allowing the user to start typing without clicking on the text field first.

I have a view controlled by a NSViewController. The view contains several text fields. The NSView and NSWindow are both custom subclasses. The text field is not subclassed. Just a standard NSTextField.

Inside awakeFromNIB for the view controller I have the code:

[[[NSApplication sharedApplication] mainWindow] makeFirstResponder:firstBox];
NSLog(@"%@",NSStringFromClass([[[[NSApplication sharedApplication] mainWindow] firstResponder] class]));

The text field gets a focus ring around it, and the NSLog prints that the first responder is an NSTextField but I still have to click inside the text field before I can begin typing.

What could cause the field to be the first responder but not editable? Is there a better method I should be calling makeFirstResponder from?

I found a potentially useful hint on CocoaDev.com , try doing this:

[[[NSApplication sharedApplication] mainWindow] 
    performSelector: @selector(makeFirstResponder:) 
         withObject: firstBox 
         afterDelay:0.0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM