Question

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?

Was it helpful?

Solution

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

[[[NSApplication sharedApplication] mainWindow] 
    performSelector: @selector(makeFirstResponder:) 
         withObject: firstBox 
         afterDelay:0.0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top