3

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?

Ian K
  • 137
  • 1
  • 10
  • is the window in front and key when you set the textfield to be first responder? – Michael Dautermann Sep 28 '13 at 18:16
  • Yes. In applicationDidFinishLaunching I make the window key and order front. The user will click a button on the first view which will switch the view controller and display the new view where the problem is occurring. – Ian K Sep 28 '13 at 18:22
  • It seems to have to do with the fact that I'm making the field first responder in awakeFromNib. If I create an IBAction that makes the NSTextField first responder and associate it to a button, when I click the button the text field becomes first responder and has the cursor in it and I can begin typing right away. – Ian K Sep 28 '13 at 18:23
  • I've also tried using loadView but it has the same results that the field has a focus ring but I cannot type in it. `- (void) loadView { [super loadView]; [[[NSApplication sharedApplication] mainWindow] makeFirstResponder:firstBox]; NSLog(@"%@",NSStringFromClass([[[[NSApplication sharedApplication] mainWindow] firstResponder] class])); }` – Ian K Sep 28 '13 at 18:27

1 Answers1

7

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

[[[NSApplication sharedApplication] mainWindow] 
    performSelector: @selector(makeFirstResponder:) 
         withObject: firstBox 
         afterDelay:0.0];
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215