문제

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top