macOS NSTextfield looses focus when NSPopover open
I am trying to make a search autocomplete in my macOS application:
I have a window with a NSTextField. When value changes, I display an NSPopover which contains NSOutlineView and the list is updated as the user is typing text. The user can select a result in the NSOutlineView then.
But, if I make NSOutlineView enabled to be able to click on a result, the NSTextField looses focus when NSPopover opens. If I set isEnabled to false for the NSOutlineView, NSTextField keeps focus but I can't select a result in the list.
Do you have any idea to keep focus on NSTextField without disable NSOutlineView ?
Thank you.
I have found a solution:
Before NSPopover.show(), I store the current textField selected range:
let range = textField.currentEditor()?.selectedRange
And after:
textField.selectText(self)
textField.currentEditor()?.selectedRange = range ?? NSMakeRange(0, 0)
If I call selectText() without setting the selected range, it will select all text. Moreover, save the selected range keeps cursor position.
链接地址: http://www.djcxy.com/p/85262.html上一篇: 无法在IB中绑定NSSlider?