Problem with NSOpenPanel & deprecated functions

Discussion in 'Mac Programming' started by tim997, Jan 13, 2010.

  1. tim997 macrumors newbie

    Jan 13, 2010
    I'm having some problems using NSOpenPanel. I have an open file dialog, which I want to only display/select files of a given type. If I use runModalForTypes everything works just fine. However, this function is deprecated and setAllowedFileTypes & runModal should be used instead. Using the new functions though does not work (all files are displayed rather than only the allowed types) and I can't figure out why. Can anyone please enlighten me?

    Here's the code:

    NSOpenPanel* openDlg = [ NSOpenPanel openPanel ];
    NSArray* fileTypes = [ NSArray arrayWithObject:@"obj" ];
    [ openDlg setAllowedFileTypes:fileTypes ]; // DOES NOT WORK
    [ openDlg runModal ];
    [ openDlg runModalForTypes:fileTypes ]; // WORKS
    I tried also setting setAllowsOtherFileTypes:NO, but this had no effect.

    Thanks for any help!
  2. kainjow Moderator emeritus


    Jun 15, 2000
    Is that the exact code you're using? Are you testing this on 10.6 or a different OS?

    I just tested this, and it worked fine (under 10.6.2)
    NSOpenPanel *o = [NSOpenPanel openPanel];
    [o setAllowedFileTypes:[NSArray arrayWithObject:@"mp3"]];
    [o runModal];
  3. tim997 thread starter macrumors newbie

    Jan 13, 2010
    To sanity check, I copied and pasted your code. Same problem. :(

    I'm under 10.5.8.
  4. kainjow Moderator emeritus


    Jun 15, 2000
    Edit: ok, so basically that method doesn't do anything for an NSOpenPanel prior to 10.6.

    NSSavePanel.h on 10.6 says:
    So, if you really don't want to use that method, you'll have to do a check at runtime for SL.
  5. tim997 thread starter macrumors newbie

    Jan 13, 2010
    Ah, I was just coming back to post an "is it a 10.5 vs 10.6 issue?" query but it seems you've already answered me.

    You'll have to forgive me, I'm used to coding under Windows. So if I get past compiling and linking, I just assume the functionality of my code is working & available.

    I'm fine with using the older function - i just hate not knowing why something's not working, even when it's a trivial issue like this.

    Thanks for the response :)

Share This Page