在NSMenuItem中使用NSProgressIndicator [英] Using NSProgressIndicator inside an NSMenuItem
问题描述
我想在状态栏菜单中使用NSProgressIndicator(不确定)。我使用NSView对象作为视图的menuitem,然后subview进度指示器显示它。但每当我尝试调用startAnimation:进度,没有什么发生。当我尝试在一个正常的NSWindow做同样的事情,它的工作完美,只是不是当在一个menuitem。
I'm trying to use a NSProgressIndicator (indeterminate) inside of a statusbar-menu. I'm using an NSView-object as view for the menuitem, and then subviews the progress indicator to display it. But whenever i try to call the startAnimation: for the progress, nothing happens. When i try do the very same thing on a normal NSWindow it works perfectly, just not when inside a menuitem.
我是新的可可和objective-c我可能忽略了一些显而易见,但我已经搜索了一些解决方法,但没有成功。我发现一些关于menuitems不能更新,而显示,你需要使用无边框的窗口。但我在任何文件中都无法确认。
I'm new to both cocoa and objective-c so I might've overlooked something "obvious" but I've searched quite a bit for a workaround but without success. I found something about menuitems cant be updated while shown and that you need to use a bordeless window instead. But I have not been able to confirm this in any documentation.
编辑:
。当使用setUsesThreadedAnimation:和从MenuDelegate的菜单WillOpen和创建一个新线程。这个线程运行一个本地方法:
Ok, almost works now. When using the setUsesThreadedAnimation: and from a MenuDelegate's menuWillOpen and creating a new thread. This thread runs a local method:
-(void) doWork(NSProgressIndicator*) p{
[p startAnimation:self];
}
这将在打开菜单时以随机。如果我直接调用 startAnimation:
而不通过 doWork:
(仍然使用新线程),它从来没有工作。不要 setUsesThreadedAnimation:
让进度条创建它自己的动画线程?
This will start the progressindicator on a random(?) basis when opening the menu. If I call startAnimation:
directly without going through doWork:
(still using a new thread), it never works. Doesn't setUsesThreadedAnimation:
make the progress-bar create it's own thread for the animation?
推荐答案
使用以下方式解决:
[progressIndicator performSelector:@selector(startAnimation:)
withObject:self
afterDelay:0.0
inModes:[NSArray
arrayWithObject:NSEventTrackingRunLoopMode]];
在菜单WillOpen:中,问题似乎一直在调用startAnimation:progressbar完成绘制本身。
Inside the menuWillOpen:, the problem seems to have been calling startAnimation: before the progressbar was finished drawing itself.
这篇关于在NSMenuItem中使用NSProgressIndicator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!