NSTextfield + NSMenu和第一个响应者 [英] NSTextfield + NSMenu and first responder

查看:362
本文介绍了NSTextfield + NSMenu和第一个响应者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自己的自动补充系统(结果是从一个sqlite数据库pull)

I'm trying to implement my own autocomplemention system (result is pull from an sqlite database)

我设置了一个NSTextField和适当的委托。每次NSTextField中的文本改变,它调用 - (void)controlTextDidChange:(NSNotification *)aNotification 方法

I've set up a NSTextField and the appropriate delegate. Each time the text in the NSTextField change, it call - (void)controlTextDidChange:(NSNotification *)aNotification method

它工作正常,在这种方法我构建一个菜单程序化,最后我调用/显示它的代码:

It work fine, in this method I build a menu programtically and finally I call/show it with that code:

 NSRect frame = [address frame];
NSPoint menuOrigin = [[address superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height-25)
                                                toView:nil];

NSEvent *event =  [NSEvent mouseEventWithType:NSLeftMouseDown
                                     location:menuOrigin
                                modifierFlags:NSLeftMouseDownMask // 0x100
                                    timestamp:0
                                 windowNumber:[[address window] windowNumber]
                                      context:[[address window] graphicsContext]
                                  eventNumber:0
                                   clickCount:1
                                     pressure:1]; 
[NSMenu popUpContextMenu:menu withEvent:event forView:address];

其中地址是我的 NSTextField 菜单是我的 NSMenu
问题是,菜单采取焦点,所以你可以在文本字段中只输入1个字母,然后你不能键入文本了,因为菜单现在是第一个响应者。

Where address is my NSTextField and menu is my NSMenu. The problem is that the menu take the focus, so you can type only 1 letter in the text field and then you can't type text anymore because the menu is now the first responder.

我的问题是如何显示菜单并将文本字段保留为第一个响应者,以便在字段中每次文本更改时重新加载菜单时可以键入。

My questions is how to show the menu and keep the text field as first responder so you can type in it while the menu is reloaded at each text change in the field.

它应该像Safari或chrome地址栏中的例子。

It should be like in Safari or chrome address bar in fact for example.

推荐答案

我不相信这是可能的NSMenu。 NSMenu实现由系统控制在相当低的水平,它被设计为采取键盘焦点。你需要的是创建自己的视图或窗口,看起来有点像菜单,但不使用NSMenu。请注意,例如,chrome地址栏中的菜单看起来不像标准的NSMenu。您需要创建一个将显示和绘制的视图,并接收回调或通知以便根据用户类型进行更新,但不会进行键盘对焦。有NSView的方法(NSResponder实际上)控制视图是否接受第一响应者状态。

I don't believe this is possible with NSMenu. NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status.

这篇关于NSTextfield + NSMenu和第一个响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆