Search code examples
objective-ccocoaxcode4.3nssavepanel

How to change a deprecated beginSheetForDirectory method


I have an app that was using beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo:.

I checked Apple documentation which said it's deprecated and to use another method instead:

Presents a Save panel as a sheet with a specified path and, optionally, a specified file in that path. (Deprecated in Mac OS X v10.6. Use beginSheetModalForWindow:completionHandler: instead.)

My question is how to change this code to the new one?

//  [savePanel setRequiredFileType:@"png"];
[savePanel beginSheetForDirectory:nil 
                             file:nil  
                   modalForWindow:[self window] 
                    modalDelegate:self 
                   didEndSelector:@selector(didEndSaveSheet:returnCode:conextInfo:) 
                      contextInfo:NULL];

Solution

  • You're looking for the beginSheetModalForWindow:completionHandler: method.

    Example:

    NSSavePanel *savePanel = [NSSavePanel savePanel];
    
    [savePanel beginSheetModalForWindow:_window completionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            NSURL *savePath = [[savePanel URLs] objectAtIndex:0];
        } else {
            [savePanel close];
        }
    }];