Pergunta

I have an NSMenu in the Mac statusbar, inside it I have a load of NSMenuItems and a custom view. Inside the custom view I have an NSTextField. I want to set the focus on the NSTextField when the menu is opened as in the Spotlight menu so the user can type straight away.

I have tried quite a few methods including:

    [myTextField becomeFirstResponder];

and

    [myTextField selectText: self];
    [[myTextField currentEditor] setSelectedRange:NSMakeRange([[myTextField stringValue] length], 0)];

but none of them work.

Thanks Alex

Foi útil?

Solução

You were on the right track with the first one, but -becomeFirstResponder doesn't actually make your view the first responder--you have to call -[NSWindow makeFirstResponder:] for that.

Google suggests that NSMenus actually have an attached window. You have to use it very carefully, but it is safe to call makeFirstResponder: on it.

More information about this and how to take advantage of it here: https://web.archive.org/web/20171113100008/http://www.cocoabuilder.com/archive/cocoa/195835-set-focus-on-nsview-in-an-nsmenuitem.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top