覆盖NSDocument窗口标题中的"已编辑"

pau*_*kow 7 cocoa nsdocument nswindow nswindowcontroller osx-lion

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

我正在使用网络服务管理自己的保存和自动保存,只是不想在标题栏中分心.

我试过压倒:

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

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

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

我也试过这个:

[[self.window standardWindowButton: NSWindowDocumentVersionsButton] setTitle:nil];
Run Code Online (Sandbox Code Playgroud)

这显示一个空白字符串而不是编辑,但破折号仍然出现 - 通常分隔文档名称和编辑.

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

Ben*_*Uri 6

几个选项:

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

    @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
    
    Run Code Online (Sandbox Code Playgroud)
  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
    
    Run Code Online (Sandbox Code Playgroud)