8

如何防止窗口标题为脏的 NSDocument 显示“已编辑”?

我正在使用网络服务管理自己的保存和自动保存,只是不想让标题栏中的注意力分散。

我试过覆盖:

  • NSDocument 的-isDocumentEdited并且-hasUnautosavedChanges总是返回NO
  • -[NSWindowController setDocumentEdited]什么都不做,或者总是使用NO而不管参数的实际值。
  • -[NSWindowController synchronizeWindowTitleWithDocumentName]什么都不做。
  • -[NSWindow setDocumentEdited]什么都不做,或者总是使用NO而不管参数的实际值。

在所有情况下,当我对保存的文档进行更改时,标题栏仍会更改为已编辑。

如果我重写-[NSDocument updateChangeCount:]并且-[NSDocument updateChangeCountWithToken:forSaveOperation:]什么都不做,我可以防止这种情况发生,但它也会影响保存、自动保存和其他文档行为。

我也试过这个:

[[self.window standardWindowButton: NSWindowDocumentVersionsButton] setTitle:nil];

这显示了一个空白字符串而不是 Edited,但破折号仍然出现 - 通常将文档名称和 Edited 分开的破折号。

知道如何从文档中撬开这部分窗口吗?

4

3 回答 3

6

几个选项:

  1. 要获得指向“破折号”的指针,请在 [window.contentView.superview.subviews] 中查找字符串值等于“-”的 TextField。您也可以将其文本设置为空字符串。

    @implementation NSWindow (DashRetrivalMethod)
    - (NSTextField*)versionsDashTextField
    {
        NSTextField* res = nil;
        NSView* themeFrame = [self.contentView superview];
        for (NSView* tmp in [themeFrame subviews])
        {
            if ([tmp isKindOfClass:[NSTextField class]])
            {
                if ([[(NSTextField*)tmp stringValue] isEqualToString:@"—"])
                {
                      res = (NSTextField*)tmp;
                      break;
                }
            }
        }
        return res;
    }
    @end
    
  2. 你可以覆盖 NSWindow 的 -setRepresentedURL:。这也会影响 NSWindowDocumentIconButton 和弹出菜单,但您可以根据需要手动创建它:[NSWindow standardWindowButton: NSWindowDocumentIconButton]。

  3. 覆盖这三个 NSDocument 的未记录方法之一:

    // Always return here NO if you don't want the version button to appear. 
    // This seems to be the cleanest options, besides the fact that you are 
    /// overriding a private method.
    - (BOOL)_shouldShowAutosaveButtonForWindow:(NSWindow*)window;
    
    // Call super with NO
    - (void)_setShowAutosaveButton:(BOOL)flag; 
    
    // Here the button and the dash are actually created
    - (void)_endVersionsButtonUpdates; 
    
    // Here Cocoa hide or unhide the edited button
    - (void)_updateDocumentEditedAndAnimate:(BOOL)flag
    
于 2012-08-17T01:53:25.287 回答
1

- (BOOL)hasUnautosavedChanges除了覆盖之外,您是否尝试过覆盖 NSDocuments - (BOOL) isDocumentEdited

于 2012-04-30T20:51:00.310 回答
0

虽然这是一个较晚的答案,但您可以通过覆盖轻松确定 NSDocument 窗口的标题

- (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName

在您的 NSWindowController 中并返回适当的标题。

您也可以通过覆盖 NSDocument 的属性来做到这一点:

- (NSString *)displayName

但Apple建议这样做,因为操作系统错误处理程序通常会使用它。

我添加了这个答案,因为其他答案都没有真正让我走上正确的道路。

于 2016-10-05T18:00:24.770 回答