NSProgressBar startAnimation called FROM a ManiMenu action
First question in StackOverflow and also Cocoa newbye and in addition I am using XCode 4 (so be kind please!)
Scenario:
A simple NSPersistentDocument Multi-windows: each document have a window.nib and an attached WindowController The application RUN a NSTask (in background) when I press a button in the WINDOW TOOLBAR.
In the window (inside a View) I decided to put a nice NSProgressIndicator (indeterminate) that ani开发者_开发百科mate when task start and stopAnimation when task end (I collect task messages trough Notifications).
Sample Code:
NSButton --> IBAction --> method in the WindowController
- (IBAction)launchSim:(id)sender
{
[simcell launchTask];
[progBar startAnimation:self];
.... more code ...
}
Everything works nice and perfectly.
Now (as usual in this scenarios) you get a strange bug and you start to become unproductive:
I decide to run the same Action also from the MainMenu (from an NSMenuItem):
In the MainMenu.xib:
NSMenuItem ("RUN") --> FirstResponder --> User Defined Action in First responder:launchSim (type id)
The RUN menu item run correctly the Task (I have NSLog DEBUG messages) BUT the animation (startAnimation) of the progressBar don't start!
THe DIFFERENCE in the 2 Actions:
- the first one (working) is called from the Nib file owned by the WindowController
- the second one (not working) is called from the MainMenu.xib and sent to FirstResponder
Both the actions execute correctly the other part of code in the method, but if I call the action from the menu I can't see any animation of the progress indicator.
What I missing?
Thanks and Best Regards
You don't need to do anything with a first responder in order to connect a menu item to an action.
Making a menu item perform an action works exactly the same way as making a button perform an action:
- Open the nib file.
- Go to the menu and navigate to your "Run" menu item that is supposed to trigger the action.
- Ctrl-Click on the menu item and drag the connecting line to the window controller.
- When you release the drag, you should see a menu pop up. Click launchSim: and you should be all set.
Well I answer to myself cause I find the bug:
The structure is the classic structure for an "advanced" NSPersistentDocument:
- one class for the document
- one subclass of the NSWindowController to control the view
this follow the MVC architecture:
- Model: the document
- Controller:attached to the window
- View: the window
My mistake was to CREATE an NSObject in the view and link him to the NSWindowController, while the correct way to do this is to SET you SubClass of the NSWindowController as File Owner and now magically, also the MainMenu start the animation. This is somehow related to the chain of responders but is far away from my current knowledge of Cocoa. If anyone can comment and explain....
精彩评论