PDA

View Full Version : How to open a file dialog before the Main window display?




MayPeng
Dec 7, 2010, 01:43 AM
I create a cocoa application, as we know it run a default window if we don't add any code. Now I want open a file dialog before the main window display.
ps. I open file dialog using method as the following:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
if([openPanel runModalStyle:nil] == NSOKButton)
{
//deal the data in the opening file
}
But I couldn't put this code onto right place. Where should I put it in order to reach my aim?



garethlewis2
Dec 7, 2010, 03:19 AM
Not sure why you need to do this, but here goes.

If you are using Snow Leopard, you already have the app delegate in XCode, you could put the code in the delegate method windowWillOpen. You will probably need to make sure that the file dialog is only opened at startup, and not every time, the user creates a new window, if you are using multiple documents. Maybe completely wrong.

Peter Maurer
Dec 7, 2010, 12:32 PM
I'd suggest unchecking the "visible on launch" checkbox for the main window, then running the open dialog in the applicationDidFinishLaunching implementation of your app delegate.

Then, once you're done with the dialog, bring the main window to the foreground manually -- if you actually still want it to appear, that is.

MayPeng
Dec 9, 2010, 09:31 PM
I'd suggest unchecking the "visible on launch" checkbox for the main window, then running the open dialog in the applicationDidFinishLaunching implementation of your app delegate.

Then, once you're done with the dialog, bring the main window to the foreground manually -- if you actually still want it to appear, that is.

You are right.I indeed will bring the main window once done with the dialog.
Now I have dealed with the problem according to what you said.I very very appreciate you!!!