在 NSMenuItem 对象上调用 setTitle 方法不会更改显示的标题,但会更改属性
将 MacRuby 与 XCode 结合使用,我希望能够更改应用程序菜单中项目的标题。
在我的简化示例中,我有一个带有按钮和标签的窗口,以及一个带有菜单项的菜单。
我创建了以下类并将 @my_label 链接到标签,@my_menu 链接到菜单项,并将按钮单击链接到 button_clicked 方法。当您单击按钮时,标签的文本会更新以显示“有人按下了按钮”,但实际显示的菜单标题并未改变。
class MyWindowController < NSWindowController
attr_accessor :my_label
attr_accessor :my_menu
def button_clicked(sender)
@my_menu.setTitle("Someone pushed the button")
@my_label.stringValue = @my_menu.title
end
end
这似乎表明 title
property 正在更新,但更改并未显示。
我查看了 NSMenuItem 的文档,但看不到任何明显的错误。然而我对此还很陌生,所以希望我只是错过了一些简单的东西。
我正在使用 MacRuby 0.10 和 XCode 4(如果这有什么区别的话)。
保罗
Using MacRuby with XCode, I would like to be able to change the title of an item in my application's menu.
In my simplified example I have window with a button and a label, and a menu with a menu item in it.
I created the following class and linked @my_label to the label, @my_menu to the menu item, and the button click to the button_clicked method. When you click on the button, the text of the label update to show "Someone pushed the button", but the actual displayed title of the menu is not changing.
class MyWindowController < NSWindowController
attr_accessor :my_label
attr_accessor :my_menu
def button_clicked(sender)
@my_menu.setTitle("Someone pushed the button")
@my_label.stringValue = @my_menu.title
end
end
This would seem to indicate that the title
property is being updated, but the change just isn't being shown.
I've looked at the docs for NSMenuItem and I can't see anything obvious that I'm doing wrong. However I'm pretty new to this so hopefully I'm just missing something simple.
I'm using MacRuby 0.10 with XCode 4 if that makes any difference.
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很奇怪。我将标签和按钮放在窗口上,链接它们,然后将菜单项放在顶部的“视图”菜单上,并将其链接。
一切正常。
你有不同的设置吗?也许是状态栏菜单?
您是否将控制器作为 xib 视图上网格左侧的对象? (我猜你是这样做的,因为你链接了所有内容,但仍然..)
也许菜单项也链接到其他地方?
菜单项会自动更新,所以这应该可以正常工作..只需再次检查连接即可。
我可以将我制作的项目发送给您进行尝试,尽管我怀疑它会有所帮助..
that's weird. I put label and button on a window, linked them, and put a menu item on the "View" menu on top, and linked it.
everything works fine.
Do you have a different setup? maybe a status bar menu?
Do you have your controller as an object on the left of the grid on the xib view? (I gues you do, since you linked everything, but still..)
maybe the menu item is linked somewhere else as well?
Menu items update automatically, so this should work fine.. just check the connections again.
I can send you the project I made to try it, although I doubt it will help..