Cocoa 如何为NSPersistentDocument窗口指定标题

Cocoa 如何为NSPersistentDocument窗口指定标题,cocoa,core-data,Cocoa,Core Data,我已将NSPersistentDocument子类化。我也重新命名了窗口。但当我运行应用程序时,应用程序窗口的标题为“Untitled”。没有-setTitle:方法可用于更改标题。你知道我该怎么做吗?你是否通过将设置标题:发送到窗口来设置标题 如果是这样,那就错了。改为设置文档的displayName。(请记住,NSPersistentDocument是NSDocument的子类)您是否通过将setTitle:发送到窗口来设置标题 如果是这样,那就错了。改为设置文档的displayName。(

我已将NSPersistentDocument子类化。我也重新命名了窗口。但当我运行应用程序时,应用程序窗口的标题为“Untitled”。没有
-setTitle:
方法可用于更改标题。你知道我该怎么做吗?

你是否通过将
设置标题:
发送到窗口来设置标题


如果是这样,那就错了。改为设置文档的
displayName
。(请记住,
NSPersistentDocument
NSDocument
的子类)

您是否通过将
setTitle:
发送到窗口来设置标题


如果是这样,那就错了。改为设置文档的
displayName
。(请记住,
NSPersistentDocument
NSDocument
的子类)

您不更改标题,用户通过保存文档来更改。

您不更改标题,用户通过保存文档来更改。

您可以将窗口标题绑定到文档,并使用键值观察来更新它

使用Interface Builder选择MyDocument.xib的“窗口”,并转到inspector中的“绑定”选项卡。选中要绑定到“文件所有者”的“标题”和要绑定到“标题”的“模型密钥路径”

然后在NSPersistentDocument的子类中添加以下代码

@interface MyDocument : NSPersistentDocument {
  NSString * _title;
}  
@end

@implementation MyDocument

//P All kinds of all your good stuff here

- (NSString *) title {
  return _title;
}

@end
现在,如果您想更改窗口的标题,可以使用KVO。比如说

- (BOOL)readFromURL:(NSURL *)absoluteURL 
             ofType:(NSString *)typeName 
              error:(NSError **)outError {

  //P All your good code

  [self willChangeValueForKey:@"title"];
  _title = [absoluteURL lastPathComponent];
  [self didChangeValueForKey:@"title"];  

  //P More good code

}

您可以将窗口的标题绑定到文档,并使用键值观察来更新它

使用Interface Builder选择MyDocument.xib的“窗口”,并转到inspector中的“绑定”选项卡。选中要绑定到“文件所有者”的“标题”和要绑定到“标题”的“模型密钥路径”

然后在NSPersistentDocument的子类中添加以下代码

@interface MyDocument : NSPersistentDocument {
  NSString * _title;
}  
@end

@implementation MyDocument

//P All kinds of all your good stuff here

- (NSString *) title {
  return _title;
}

@end
现在,如果您想更改窗口的标题,可以使用KVO。比如说

- (BOOL)readFromURL:(NSURL *)absoluteURL 
             ofType:(NSString *)typeName 
              error:(NSError **)outError {

  //P All your good code

  [self willChangeValueForKey:@"title"];
  _title = [absoluteURL lastPathComponent];
  [self didChangeValueForKey:@"title"];  

  //P More good code

}