How to open a file dialog before the Main window display?

Discussion in 'Mac Programming' started by MayPeng, Dec 6, 2010.

  1. MayPeng macrumors member

    Nov 21, 2010
    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?
  2. garethlewis2 macrumors 6502

    Dec 6, 2006
    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.
  3. Peter Maurer macrumors member

    Oct 9, 2008
    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.
  4. MayPeng thread starter macrumors member

    Nov 21, 2010
    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!!!

Share This Page