Set focus on NSTextField in NSMenu?

I have NSMenu in the Mac status bar, inside it I have NSMenuItems loading and user view. Inside the user view, I have an NSTextField. I want to focus on NSTextField when the menu opens, as in the Spotlight menu so that the user can immediately enter text.

I tried several methods, including:

    [myTextField becomeFirstResponder];

and

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

but none of them work.

Thanks Alex

+3
source share
1 answer

You were on the right track with the first, but -becomeFirstResponderreally does not make your opinion the first defendant - you should call -[NSWindow makeFirstResponder:]for this.

Google , NSMenu . , makeFirstResponder:.

, : http://www.cocoabuilder.com/archive/cocoa/195835-set-focus-on-nsview-in-an-nsmenuitem.html

+2

Source: https://habr.com/ru/post/1784797/


All Articles