Kosar
Kosar

Reputation: 190

NSAlert Over NSSavePanel

i am new in objective c , i want create alert over save panel to get confirm from user to overwrite exist file or not , like text editor when you save a file in a directory that have same file name alert show over save panel that ask to replace or cancel , when user select cancel alert disappear , when user select replace the alert disappear then save panel. not save panel disappear then alert show. please help

Upvotes: 0

Views: 1059

Answers (1)

Andrew Madsen
Andrew Madsen

Reputation: 21373

This code is tested and works. It will warn you if the file already exists. The save panel and the file replacement alert will be appear in whatever language Mac OS X is set to use (see screenshot for Japanese example):

- (IBAction)saveTestFile:(id)sender 
{
    NSString *saveString = [NSString stringWithFormat:@"Hello World, it's %@!", [NSDate date]];
    NSSavePanel *savePanel = [NSSavePanel savePanel];
    if ([savePanel runModal] == NSFileHandlingPanelOKButton)
    {
        NSURL *saveURL = [savePanel URL];
        NSError *error = nil;
        if (![saveString writeToURL:saveURL atomically:YES encoding:NSASCIIStringEncoding error:&error])
        {
            NSLog(@"Unable to save file: %@", error);
        }
    }
}

Japanese Save Panel enter image description here

Upvotes: 1

Related Questions