Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOpenPanel - Everything deprecated?

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?

like image 890
Cole Avatar asked Oct 07 '11 23:10

Cole


2 Answers

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];
}];
like image 186
Guillaume Avatar answered Oct 13 '22 01:10

Guillaume


As far as I know, you can use the runModal method like shown below:

NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];

if ([openPanel runModal] == NSOKButton)
{
    NSString *selectedFileName = [openPanel filename];
}
like image 35
Jesse Dunlap Avatar answered Oct 12 '22 23:10

Jesse Dunlap