[英]Using NSProgressIndicator inside an NSMenuItem
I'm trying to use a NSProgressIndicator (indeterminate) inside of a statusbar-menu. 我正在尝试在状态栏菜单中使用NSProgressIndicator(不确定)。 I'm using an NSView-object as view for the menuitem, and then subviews the progress indicator to display it. 我正在使用NSView对象作为menuitem的视图,然后查看进度指示器以显示它。 But whenever i try to call the startAnimation: for the progress, nothing happens. 但每当我尝试调用startAnimation时:对于进度,没有任何反应。 When i try do the very same thing on a normal NSWindow it works perfectly, just not when inside a menuitem. 当我尝试在普通的NSWindow上做同样的事情时,它可以很好地工作,而不是在一个menuitem中。
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. 我是cocoa和objective-c的新手,所以我可能会忽略一些“显而易见的”但我已经搜索了很多但是没有成功。 I found something about menuitems cant be updated while shown and that you need to use a bordeless window instead. 我发现一些关于menuitems的东西在显示时无法更新,你需要使用无边框窗口。 But I have not been able to confirm this in any documentation. 但我无法在任何文档中证实这一点。
Edit: 编辑:
Ok, almost works now. 好的,现在差不多了。 When using the setUsesThreadedAnimation: and from a MenuDelegate's menuWillOpen and creating a new thread. 当使用setUsesThreadedAnimation:并从MenuDelegate的menuWillOpen中创建一个新线程。 This thread runs a local method: 该线程运行本地方法:
-(void) doWork(NSProgressIndicator*) p{
[p startAnimation:self];
}
This will start the progressindicator on a random(?) basis when opening the menu. 这将在打开菜单时以随机(?)为基础启动progressindicator。 If I call startAnimation:
directly without going through doWork:
(still using a new thread), it never works. 如果我直接调用startAnimation:
而不通过doWork:
仍然使用新线程),它永远不会工作。 Doesn't setUsesThreadedAnimation:
make the progress-bar create it's own thread for the animation? 没有setUsesThreadedAnimation:
让进度条为动画创建自己的线程?
Solved it by using: 通过使用解决它:
[progressIndicator performSelector:@selector(startAnimation:)
withObject:self
afterDelay:0.0
inModes:[NSArray
arrayWithObject:NSEventTrackingRunLoopMode]];
Inside the menuWillOpen:, the problem seems to have been calling startAnimation: before the progressbar was finished drawing itself. 在menuWillOpen:中,问题似乎是调用startAnimation:在进度条完成绘制之前。
How are you referencing the NSProgressIndicator
that is in the view (and the one in the window, for that matter)? 你如何引用视图中的NSProgressIndicator
(以及窗口中的那个)? For example, do you have a controller class that has IBOutlet
's hooked up to the progress indicators? 例如,您是否有一个控制器类,其IBOutlet
与进度指示器相连? If you are using an IBOutlet
, are you sure it's hooked up properly in the nib file? 如果您使用的是IBOutlet
,您确定它已在nib文件中正确连接吗?
Also, where and when are you calling startAnimation:
? 另外,你在何时何地调用startAnimation:
(We need to see some code). (我们需要看一些代码)。
One thing that can sometimes happen is that you forget to hook up an IBOutlet
in the nib. 有时可能发生的一件事是你忘记在笔尖中连接一个IBOutlet
。 Then, when you attempt to tell the object to do something in code at runtime, the IBOutlet
is nil
, and so what you think is a message being sent to your object is in fact, a message being sent to nil
. 然后,当您尝试告诉对象在运行时在代码中执行某些操作时, IBOutlet
nil
,因此您认为发送到对象的消息实际上是发送给nil
的消息。 In other words, it's just ignored, and effectively looks like it's not working. 换句话说,它只是被忽略了,实际上看起来它不起作用。
Provided you do have a (potentially) valid reference to the UI object, the other common issue you'll see is when a developer is trying to send a message to the object at "too early" of a time. 如果您对UI对象有(可能)有效的引用,那么您将看到的另一个常见问题是当开发人员试图在“太早”的时间向该对象发送消息时。 In general, init
methods are too early in the controller object's lifetime to be able to send messages to user interface objects—those IBOutlet
's are still nil
. 通常, init
方法在控制器对象的生命周期中太早,无法向用户界面对象发送消息 - 这些IBOutlet
仍然是nil
。 By the time -awakeFromNib
is called, IBOutlet
's should be valid (provided you hooked them up in IB) and you can then send the message to the UI object. 在调用-awakeFromNib
, IBOutlet
应该是有效的(假设您将它们连接到IB中),然后您可以将消息发送到UI对象。
你有没有通过-setUsesThreadedAnimation告诉它使用线程动画?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.