Question

I’m trying to figure out how to get a spinner to animate on an NSPopupButton’s menu item. 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. 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

Was it helpful?

Solution

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.

How are you referencing the NSProgressIndicator that is in the view (and the one in the window, for that matter)? For example, do you have a controller class that has IBOutlet's hooked up to the progress indicators? If you are using an IBOutlet, are you sure it's hooked up properly in the nib file?

Also, where and when are you calling 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. 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. 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. 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top