NSOpenPanel Sheet
Asked Answered
N

1

5

This is how I display an open panel as a floating window.

Can someone help me out with running the panel as a sheet ? The window object is mWindow. Much of the standard code I was using is depreciated.

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"mp3", @"mp2", @"m4a", nil];

[openPanel setAllowsMultipleSelection: NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanCreateDirectories:NO];
[openPanel setCanChooseFiles:YES];
[openPanel setAllowedFileTypes:fileTypes];

NSString * filePath = @"~/Desktop";
filePath = [filePath stringByExpandingTildeInPath];
NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 
[openPanel setDirectoryURL:fileURL];

NSInteger clicked = [openPanel runModal];
if (clicked == NSFileHandlingPanelOKButton) {
    for (NSURL *url in [openPanel URLs]) {
        NSString *urlString = [url path];
        [input setStringValue:urlString];
        NSString *myString = [input stringValue];
        NSString *oldPath = [myString lastPathComponent];
        [inputDisplay setStringValue:oldPath];
    }
}
Nell answered 9/2, 2012 at 21:0 Comment(0)
U
12

Pretty simple, it's right there in the docs though you may have missed it because the relevant method is actually part of NSSavePanel, from which NSOpenPanel inherits.

Presuming you're targeting Snow Leopard or better and thus have access to blocks, it's just a matter replacing your runModal call with this:

[openPanel beginSheetModalForWindow:mWindow completionHandler:^(NSInteger result) {

    if (result == NSFileHandlingPanelOKButton) {

        // Do something.
    }
}];
Ushijima answered 9/2, 2012 at 21:11 Comment(3)
Sorry. I was simply omitting another } after ]; Working fine now. thanks for the help.Nell
mWindow ? what's mWindow ?Shutter
@AviramNetanel, it's the window on which you want the save panel to be a sheet.Stacee

© 2022 - 2024 — McMap. All rights reserved.