我有一个 NSMenuItem
,我需要更新它以显示进度(就像时间机器对其备份所做的那样)。问题是,当我在该 NSMenuItem
上设置新的 title
时,title
没有改变。
当我关闭并重新打开菜单时,它实际上正在发生变化,但我想在用户查看它时更新它。
我还尝试删除一个项目并重新插入它,但没有结果。
有什么指点吗?
最佳答案
如果您的更新代码在 run loop mode 中运行,这实际上不需要额外的努力。在菜单跟踪期间使用。这是 NSEventTrackingRunLoopMode,但您可能只想使用 NSRunLoopCommonModes,以便下拉菜单时菜单项标题正确。
这是一个菜单项 foo
的简单示例,用于计算自应用程序启动以来的秒数:
- (void)doStuff;
{
static int i = 0;
[foo setTitle:[NSString stringWithFormat:@"%d", ++i]];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[self methodSignatureForSelector:@selector(doStuff)]];
[invocation setTarget:self];
[invocation setSelector:@selector(doStuff)];
[[NSRunLoop mainRunLoop] addTimer:[NSTimer timerWithTimeInterval:1 invocation:invocation repeats:YES] forMode:NSRunLoopCommonModes];
}
关于cocoa - 显示主机菜单时更新 NSMenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6301338/