【问题标题】:change NSMenuItem title with Swift使用 Swift 更改 NSMenuItem 标题
【发布时间】:2016-08-24 16:02:22
【问题描述】:

我想在登录过程成功完成后以及用户注销后更改 MenuItem 的标题。

但是如果我登录并检查菜单项的标题,它并没有改变。我什至试图隐藏它,但没有成功。

这是我的代码

var mainMenu:NSMenu
var LoginButton:NSMenuItem
mainMenu=NSApplication.sharedApplication().mainMenu!
LoginButton=mainMenu.itemWithTitle("Login")!
LoginButton.title="LogOff"
LoginButton.hidden=true

感谢您的帮助。

【问题讨论】:

    标签: swift macos nsmenu nsmenuitem


    【解决方案1】:

    将 Interface Builder / Bindings Inspector (⌥⌘7) 中菜单项的Title 参数绑定到AppDelegate 中具有dynamic 属性的属性。

    例如在AppDelegate中声明

    @objc dynamic var logonTitle = "Login"
    

    并将菜单项的Title参数绑定到AppDelegate > Model Key Path logonTitle

    现在,每当您更改 logonTitle 的值时,标题都会相应更改。

    不需要更多的参考资料、网点等。

    PS:请按照指南中的建议以小写字母开头一致地命名您的变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多