Using NSProgressIndicator inside NSMenuItem

I am trying to use NSProgressIndicator (undefined) inside the status menu. I use an NSView object as a view for a menu item, and then look at the progress bar to display it. But whenever I try to call startAnimation: nothing happens to progress. When I try to do the same in a regular NSWindow, it only works fine when inside a menu item.

I am new to both cocoa and objective-c, so I might have missed something β€œobvious”, but I was looking for quite a bit to work around, but to no avail. I found something about the fact that menu items cannot be updated while they are showing, and that you need to use a limitless window instead. But I could not confirm this in any documentation.

Edit:

OK, it almost works now. When using setUsesThreadedAnimation: and from the MenuDelegateWillOpen menu and creating a new stream. This thread executes the local method:

-(void) doWork(NSProgressIndicator*) p{ [p startAnimation:self]; } 

When you open the menu, this will begin to indicate the progressinicator on a random (?) Basis. If I call startAnimation: directly without passing doWork: (still using the new thread), it never works. Does setUsesThreadedAnimation: progress bar make its own thread for animation?

+6
objective-c cocoa nsview nsmenuitem nsprogressindicator
source share
3 answers

Solved it using:

 [progressIndicator performSelector:@selector(startAnimation:) withObject:self afterDelay:0.0 inModes:[NSArray arrayWithObject:NSEventTrackingRunLoopMode]]; 

Inside the WillOpen: menu, the problem seemed to trigger startAnimation: before the progress bar finished drawing.

+16
source share

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

Also, where and when do you call startAnimation: :? (We need to see some code).

One thing that can sometimes happen is that you forget to plug in an IBOutlet at the tip. Then, when you try to tell the object to do something in the code at runtime, IBOutlet is nil , and therefore, in your opinion, the message sent to your object is actually a message sent to nil . In other words, it is simply ignored and effectively looks as if it is not working.

If you have a (potentially) valid reference to the user interface object, another common problem that you will see is when the developer tries to send a message to the object "too early". In general, init methods are too early in the life of the controller object to be able to send messages to user interface objects β€” those IBOutlet are still nil . At the time of the call, -awakeFromNib IBOutlet must be valid (provided that you have connected them to IB), and then you can send a message to the user interface object.

+1
source share

You said to use threaded animation through -setUsesThreadedAnimation :?

0
source share

All Articles