NSMenuItem 标题随时间变化。

发布于 2024-09-30 14:09:16 字数 575 浏览 4 评论 0原文

我想实现一个 NSStatusItem,其中标题随着时间的推移而变化。我的最终目标是在菜单栏中我设置的图像旁边显示倒计时器。也许我的处理方式是错误的。但我已经有一串随着时间不断变化的字符,代表计时器。我试图弄清楚如何将该字符串放置在为我创建的菜单设置的图像旁边。

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setTitle:"29:30"];

我已尝试多次调用 setTitle 方法,但它们似乎不起作用。只有 setTitle 的初始调用在对象初始化之前有效。之后什么也没有发生。我想我可以释放 NSStatusItem 对象并在每次我想要更改计时器时重新创建它,但这似乎非常低效。我什至不确定它是否有效。

我还考虑过使用 setToolTip 方法来显示计时器倒计时。但这似乎与 setTitle 方法相同。一旦设置,您就无法更改它,直到释放 NSStatusItem 对象并创建一个新对象为止。

任何建议将不胜感激。

谢谢

戴夫

I would like to implement an NSStatusItem where the title changes over time. My ultimate goal is to display a countdown timer in the menu bar beside the image which I have set. Maybe I am going about this the wrong way. But I already have a string of characters which are constantly changing over time which represents the timer. I'm trying to figure out how I can place that string beside the image I have set for a menulet I created.

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setTitle:"29:30"];

I have tried multiple calls of the setTitle method but they don't seem to work. Only the initial call of setTitle works prior to the object initialization. After that nothing happens. I guess I could release the NSStatusItem Object and recreate it every time I want the timer changed, but that seems very inefficient. I'm not even sure if it would work.

Also I have contemplated using the setToolTip method to display the timer countdown. But that seems to be the same as the setTitle method. Once it is set, you can't change it till you release the NSStatusItem object and create a new one.

Any suggestions would be appreciated.

Thank you

Dave

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

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

发布评论

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

评论(2

桃气十足 2024-10-07 14:09:16

我回答了我自己的问题。 -setTitle 可以多次调用,以便动态更改倒计时器的标题,不需要自定义视图。我的问题是计时器无法正常运行,并且菜单的长度不足以显示计时器的文本。我现在可以调用 setTitle 来刷新倒计时并每秒创建动态工具提示。我原本以为通过标准方法setTitle是不可能的。我错了。

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setTitle:"29:30"];

戴夫

I answered my own question. -setTitle can be called multiple times in order to change the title dynamically for the countdown timer, a custom view is not required. My problem ran into my timer not functioning correctly and the length of my menulet not being sufficient to display the text of the timer. I can now call setTitle to refresh the countdown and create dynamic tooltips every second. I originally thought it wasn't possible through the standard methods setTitle. I was wrong.

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setTitle:"29:30"];

Dave

心在旅行 2024-10-07 14:09:16

为什么不使用它的-setView:?您可以创建自己的自定义视图并根据需要进行更新。

Why not use it's -setView:? You can create your own custom view and update it however you like.

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