基于 Cocoa 文档的应用程序中文档的默认保存位置

发布于 2024-11-02 14:37:51 字数 1661 浏览 1 评论 0原文

我创建了一个基于 Cocoa 文档的绘图应用程序。我希望在“保存/另存为”对话框中使用我的应用程序创建的新文档的默认位置应位于 ~/Pictures/MyAppName/ 目录中。

我怎样才能实现这个目标?

我或多或少尝试了 Ole 下面的建议,但它不起作用。这是我的prepareSavePanel 的实现。我做错了什么?

- (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
{
    if ([self fileURL] == nil) {
        //new, not saved yet
        [savePanel setExtensionHidden:NO];

        //set default save location        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);

        if ([paths count] > 0) {
            NSString *userPicturesPath = [paths objectAtIndex:0];
            NSString *myDirPath = [userPicturesPath stringByAppendingPathComponent:@"MyAppName"];


            //create directory is it doesn't already exist
            NSFileManager *fileManager = [NSFileManager defaultManager];
            BOOL isDir;
            BOOL useMyAppDir = NO;
            if([fileManager fileExistsAtPath:myDirPath isDirectory:&isDir]){
                if (isDir) {
                    useMyAppDir = YES;
                }
            } else {
                //create the directory
                if([fileManager createDirectoryAtPath:myDirPath withIntermediateDirectories:YES attributes:nil error:nil]){
                    useMyAppDir = YES;
                }
            }

            if (useMyAppDir) {
                NSURL * myAppDirectoryURL = [NSURL URLWithString:myDirPath];
                [savePanel setDirectoryURL:myAppDirectoryURL];
            }
        }
    } else {
        [savePanel setExtensionHidden:[self fileNameExtensionWasHiddenInLastRunSavePanel]];
    }

    return YES;
}

I have created a Cocoa document based picture drawing application. I want that the default location of a new document created using my app in Save/Save As dialog should be in ~/Pictures/MyAppName/ directory.

How can I achieve this?

I tried more or less what Ole suggested below, but it doesn't work. Here is my implementation of prepareSavePanel. What am I doing wrong?

- (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
{
    if ([self fileURL] == nil) {
        //new, not saved yet
        [savePanel setExtensionHidden:NO];

        //set default save location        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);

        if ([paths count] > 0) {
            NSString *userPicturesPath = [paths objectAtIndex:0];
            NSString *myDirPath = [userPicturesPath stringByAppendingPathComponent:@"MyAppName"];


            //create directory is it doesn't already exist
            NSFileManager *fileManager = [NSFileManager defaultManager];
            BOOL isDir;
            BOOL useMyAppDir = NO;
            if([fileManager fileExistsAtPath:myDirPath isDirectory:&isDir]){
                if (isDir) {
                    useMyAppDir = YES;
                }
            } else {
                //create the directory
                if([fileManager createDirectoryAtPath:myDirPath withIntermediateDirectories:YES attributes:nil error:nil]){
                    useMyAppDir = YES;
                }
            }

            if (useMyAppDir) {
                NSURL * myAppDirectoryURL = [NSURL URLWithString:myDirPath];
                [savePanel setDirectoryURL:myAppDirectoryURL];
            }
        }
    } else {
        [savePanel setExtensionHidden:[self fileNameExtensionWasHiddenInLastRunSavePanel]];
    }

    return YES;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-11-09 14:37:51

在您的 NSDocument 子类中,覆盖 -prepareSavePanel:

- (BOOL) prepareSavePanel:(NSSavePanel *)savePanel 
{
    // Set default folder if no default preference is present
    NSDictionary *userDefaults = [[NSUserDefaults standardUserDefaults] persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
    if ([userDefaults objectForKey:@"NSNavLastRootDirectory"] == nil) {
        NSArray *picturesFolderURLs = [[NSFileManager defaultManager] URLsForDirectory:NSPicturesDirectory inDomains:NSUserDomainMask];
        if ([picturesFolderURLs count] > 0) {
            NSURL *picturesFolderURL = [[picturesFolderURLs objectAtIndex:0] URLByAppendingPathComponent:@"MyAppName"];
            [savePanel setDirectoryURL:picturesFolderURL];
        }
    }
    return YES;
}

In your NSDocument subclass, override -prepareSavePanel:

- (BOOL) prepareSavePanel:(NSSavePanel *)savePanel 
{
    // Set default folder if no default preference is present
    NSDictionary *userDefaults = [[NSUserDefaults standardUserDefaults] persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
    if ([userDefaults objectForKey:@"NSNavLastRootDirectory"] == nil) {
        NSArray *picturesFolderURLs = [[NSFileManager defaultManager] URLsForDirectory:NSPicturesDirectory inDomains:NSUserDomainMask];
        if ([picturesFolderURLs count] > 0) {
            NSURL *picturesFolderURL = [[picturesFolderURLs objectAtIndex:0] URLByAppendingPathComponent:@"MyAppName"];
            [savePanel setDirectoryURL:picturesFolderURL];
        }
    }
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文