简体   繁体   English

NSPopupButton内的NSProgressIndicator

[英]NSProgressIndicator inside an NSPopupButton

I'm trying to figure out how to get a spinner to animate on an NSPopupButton's menu item. 我试图弄清楚如何使微调器在NSPopupButton的菜单项上进行动画处理。 I can't get the spinner to animate. 我无法让微调器进行动画处理。 It appears but it doesn't do anything. 它出现了,但没有执行任何操作。

So far what I have is, I've added the spinner to the menu item like so: 到目前为止,我已经将微调器添加到菜单项中,如下所示:

_spinner = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0,0,16,16)];
[_spinner setIndeterminate:YES];
[_spinner setStyle:NSProgressIndicatorSpinningStyle];
[_spinner setControlSize:NSSmallControlSize];
[_spinner setDisplayedWhenStopped:YES];
[_spinner setHidden:NO];

NSMenuItem *item = [[button menu] itemWithTag:0];
[item setView:_spinner];

I'm listening for an NSPopUpButtonWillPopUpNotification. 我正在听NSPopUpButtonWillPopUpNotification。 When I get the notification I do this but the spinner appears and doesn't animate: 当我收到通知时,我会执行此操作,但微调框出现并且没有动画:

[_spinner setUsesThreadedAnimation:YES];
[_spinner startAnimation:nil];

I edited this to update my problem 我对此进行了编辑以更新我的问题

Try this way : 尝试这种方式:

[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仍然无效。 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对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM