如何在菜单栏中获得类似聚光灯的文本输入效果?

发布于 2024-11-24 12:29:07 字数 159 浏览 2 评论 0原文

我想在我的 Mac 应用程序的菜单栏中有一个图标 - 并且该图标应在单击时生成一个菜单。虽然菜单中有更多条目,但我希望将顶行作为通用文本输入字段 - 就像在 Spotlight 中一样。

是否可以将这样的字段添加到 NSMenu 中?或者我应该把它做成面板型窗口?

I want to have an icon in the menubar in my Mac app - and the icon should spawn a menu upon clicking. While having more entries in the menu, I would like to have a top row as a universal text entry field - like it is in Spotlight.

Is it possible to add such a field to NSMenu? Or should I do it as a panel-type window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-12-01 12:29:07

如果您使用的是 xcode 4 ,请在界面生成器中创建自定义视图并添加文本字段或任何您想要的内容。在 IB 中,还可以从对象库中拖放一个“菜单”,其中包含任意数量的项目。然后,只需按住 Ctrl 键并单击要放入文本字​​段的菜单项(在您的情况下,它将是顶部的菜单项),然后拖动到自定义视图并选择“视图”。现在,当您打开菜单时,它不会显示该空间中的菜单项,而是显示自定义视图中的所有内容。

编辑:至于你的评论,这是你应该做的。通过打开助理编辑器视图并按住 Ctrl 键并从菜单中单击到要使用的头文件,使菜单成为一个出口。现在,只需创建一个每当菜单打开时就会运行的方法,苹果已经做了这个,它被称为 menuWillOpen。

- (void)menuWillOpen: nameOfYourMenu{

[self performSelector:@selector(methodExecutedWhenMenuIsClicked) withObject:nil afterDelay:0.0 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

0 处的延迟将使其立即发生,它必须在通用模式运行循环中完成,以便菜单即使在打开时也会更新。现在只需创建 methodExecutedWhenMenuIsClicked 并设置它以便文本字段做出响应。

- (void)methodExecutedWhenMenuIsClicked{

[[yourTextfiled window] makeFirstResponder:yourTextField];

If you're using xcode 4 , make a custom view in interface builder and add a textfield or anything you want to it. In IB also drag and drop a "Menu" from the objects library with as many items as you want in it. Then simply ctrl+click the menu item you want to make into the text field (In your case it would be the top one) and drag to the custom view and select "view". Now when you open the menu, instead of showing a menu item in that space, it shows whatever was in your custom view.

EDIT: As for your comment here's what you should do. Make your menu an outlet by opening the assistant editor view and ctrl+click from your menu to the header file that you want to use. now, simply make a method that will run whenever the menu will open, conveniently apple already made this, it's called menuWillOpen.

- (void)menuWillOpen: nameOfYourMenu{

[self performSelector:@selector(methodExecutedWhenMenuIsClicked) withObject:nil afterDelay:0.0 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

the delay at 0 will make it happen immediately, it must be done in the common modes run loop so that the menu will be updated even while it's open. Now just make the methodExecutedWhenMenuIsClicked and set it so the text field responds.

- (void)methodExecutedWhenMenuIsClicked{

[[yourTextfiled window] makeFirstResponder:yourTextField];
々眼睛长脚气 2024-12-01 12:29:07

您可以使用 -[NSMenuItem setView:] 将任何视图放入菜单中。请参阅 NSMenuItem.h 中的长注释以及应用程序菜单和弹出列表编程主题中的菜单中的视图部分。

You can put any view in a menu using -[NSMenuItem setView:]. See the long comment in NSMenuItem.h and the section Views in Menus in Application Menu and Pop-up List Programming Topics.

顾忌 2024-12-01 12:29:07

你可能会很挣扎。我只是尝试做同样的事情,并阅读 Ahruman 引用的应用程序菜单和弹出列表编程主题文档中的菜单中的视图,我发现了这一点:

菜单项中的视图可以正常接收所有鼠标事件,但不支持键盘事件。在“非粘性”菜单跟踪(即按住鼠标按钮操作菜单)期间,菜单项中的视图接收 mouseDragged: 事件。

我想我们是SOL。显然,Spotlight 会弹出一个无边框窗口。

You're probably going to struggle quite a bit. I just tried doing the same thing, and reading the Views in Menus in Application Menu and Pop-up List Programming Topics document referenced by Ahruman, I found this:

A view in a menu item can receive all mouse events as normal, but keyboard events are not supported. During “non-sticky” menu tracking (that is, manipulating menus with the mouse button held down), a view in a menu item receives mouseDragged: events.

I think we're SOL. Apparently Spotlight pops up a borderless window instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文