【问题标题】:Untitled Document open from template从模板打开无标题文档
【发布时间】:2014-04-24 13:10:50
【问题描述】:

在无标题文档的位置,我想在我的应用程序包中加载给定文档并让它表现得像无标题

在 NSDocumentController 中覆盖此方法,有效,但有一个问题,一旦我关闭无标题文档,文件就会从包中删除...

- (id)makeUntitledDocumentOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {

    NSString * extension = @"aaa";
    NSString * untitledFilePath = [[NSBundle mainBundle] pathForResource:@"untitled" ofType:extension];
    NSURL * sourceUrl = [[NSURL alloc] initFileURLWithPath:untitledFilePath];

    Document * document = [[Document alloc] initForURL:nil withContentsOfURL:sourceUrl ofType:typeName error:outError];
    return document;
}

如何从 app bundle 中保存的“模板”文档中“加载”未命名的文档?

【问题讨论】:

    标签: macos cocoa nsdocument


    【解决方案1】:

    -initForURL:withContentsOfURL:ofType:error:nil 作为初始URL 调用时,这通常表明sourceURL 将是一个未保存的自动保存文件,如果您最终不保存将被删除(如果你保存,它会被移动)。

    我建议您初始化文档,然后使用文档的read 方法之一来读取内容并覆盖空文档。如果您希望在这样做后轻松放弃(即如果您不更改模板文档则没有保存提示),您可能需要使用updateChangeCount: 设置NSChangeCleared

    例如(插入到您当前的 Document init 行的位置:

    NSError *error;
    Document *document = [[Document alloc] initWithType: extension error: &error];
    if (document) {
        // load template
        if (![document readFromURL: sourceURL ofType: extension error: &error]) {
             // do something appropriate to abort the load
        }
        [document updateChangeCount: NSChangeCleared]; // don't prompt for save w/o changes
    }
    return document;
    

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      相关资源
      最近更新 更多