NSSavePanel-Object C组件-创建文件对话框-创建文件

1. beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result), 创建窗口显示在操作界面的下面

    
    NSSavePanel*    panel = [NSSavePanel savePanel];
    [panel setNameFieldStringValue:@"Create File"];
    [panel setMessage:@"Choose the path to save the document"];
    [panel setAllowsOtherFileTypes:YES];
    //[panel setAllowedFileTypes:@[@".txt"]];//设置新建文件默认的后缀,默认是无后缀需自己添加
    [panel setExtensionHidden:YES];
    [panel setCanCreateDirectories:YES];
    [panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton) {//NSFileHandlingPanelOKButton
            NSString *path = [[panel URL] path];
            [@"Hello World!" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
    }];

2. beginWithCompletionHandler:^(NSInteger result),创建界面显示在屏幕的中央

    NSSavePanel*    panel = [NSSavePanel savePanel];
    [panel setNameFieldStringValue:@"Create File"];
    [panel setMessage:@"Choose the path to save the document"];
    [panel setAllowsOtherFileTypes:YES];
    //[panel setAllowedFileTypes:@[@".txt"]];//设置新建文件默认的后缀,默认是无后缀需自己添加
    [panel setExtensionHidden:YES];
    [panel setCanCreateDirectories:YES];
    [panel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {//NSFileHandlingPanelOKButton
            NSString *path = [[panel URL] path];
            [@"Hello World!" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
    }];

 

posted @ 2017-09-27 17:09  笑面浮屠  阅读(506)  评论(0编辑  收藏  举报