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

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top