3

I'm working on an app that presents an NSPopover containing a number of NSTextFields. While I can tab between these fields as I expect, the popover is selecting a particular text field to be in the editing state when it appears, and it's not the field I want to edit; I'd like to be able to define which text field is editing on popover appearance programmatically (or in Interface Builder). How can I do this?

I've set up the appropriate key view loop by connecting IB outlets for all the various text fields involved, and I've hooked up the popover's nextResponder property to the text field I want to edit first, but that doesn't seem to have an effect - the popover will still select its preferred text field instead of mine. The Window Programming Guide suggests that I set the initialFirstResponder outlet of the window to the view I want selected, but an NSPopover is not an NSWindow and has no initialFirstResponder property (unless I'm missing something obvious).

Is there any way to specify which NSTextField I want to be editing when an NSPopover appears?

cacau
  • 3,606
  • 3
  • 21
  • 42
Tim
  • 59,527
  • 19
  • 156
  • 165
  • I don't have time to build a test, but once displayed, the popover _is_ a window. In the delegate's `popoverDidShow:` could you get an `NSWindow` from the NSTextField, then tell that window to `makeFirstResponder` with the text field? – Smilin Brian Sep 26 '12 at 18:13
  • @SmilinBrian: That's the workaround I'm using now, but the problem with running it in `-popoverDidShow:` is that there's a brief (but noticeable) period where the popover's default field is selected before it jumps to my chosen field. I also tried `-popoverWillShow:`, but changing the first responder in the popover's window doesn't seem to work at that point - no change is made. – Tim Sep 26 '12 at 18:49
  • Frustrating, but useful information. Sorry I can't help further. – Smilin Brian Sep 26 '12 at 21:13
  • No problem - thanks for taking the time to look! – Tim Sep 26 '12 at 22:50

2 Answers2

4

I think you said you tried using -makeFirstResponder: and passing the text field. This will set the window's firstResponder, but that's not the same as initialFirstResponder and the window must have initialFirstResponder set to something other than nil in order to respect the key view loop. (Source) A slight tweak to what you tried worked for me:

- (void)popoverWillShow:(NSNotification *)notification
{
    // Set the window's initialFirstResponder so that the key view loop isn't auto-recalculated.
    [[myField window] setInitialFirstResponder:myField];
}
Nick K9
  • 3,885
  • 1
  • 29
  • 62
0

I think you can make this work by setting all the text field's that you don't want to have focus to "selectable" instead of "Editable" in IB, this should leave the one text field you want to start with as the first responder. Then, in your popoverDidShow: method, set them all back to editable, and you should be able to tab between them as usual.

rdelmar
  • 103,982
  • 12
  • 207
  • 218