문제

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