NSOpenPanel - Everything deprecated?

Cole picture Cole · Oct 8, 2011 · Viewed 10.9k times · Source

I've been trying to get a window to show up asking the person to choose a file, and I eventually did. The problem is, Xcode complains that the method I'm using is deprecated. I looked in the class reference, but everything under the "running panels" section has been deprecated as of Mac OS 10.6. Is there a different class I'm supposed to be using now?

Answer

Guillaume picture Guillaume · Dec 27, 2011

In 10.6, there was a few changes to this classes. One of the benefits is that there is now a block-based API.

Here is a code snippet on how to use that:

NSOpenPanel *panel = [[NSOpenPanel openPanel] retain];

// Configure your panel the way you want it
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:NO];
[panel setAllowsMultipleSelection:YES];
[panel setAllowedFileTypes:[NSArray arrayWithObject:@"txt"]];

[panel beginWithCompletionHandler:^(NSInteger result){
    if (result == NSFileHandlingPanelOKButton) {

        for (NSURL *fileURL in [panel URLs]) {
            // Do what you want with fileURL
            // ...
        }
    }

    [panel release];
}];