【问题标题】:NSTextView how to disable 'You don’t own the file “xxx” and don’t have permission to write to it.'NSTextView 如何禁用'您不拥有文件“xxx”并且没有写入权限。'
【发布时间】:2018-11-07 19:07:54
【问题描述】:

我的基于文档的应用程序中有一个NSTextView。如果我打开一个我只有读取权限的文件并修改 NSTextView 的内容,我会收到一个对话框,说明:

您不拥有“xxx”文件,也没有写入权限。

但是这个文本视图实际上并没有与我打开的文档进行交互。是否有可能以某种方式取消 NSTextView 的这种行为?

【问题讨论】:

  • 我找到了一个涉及调配私有方法的解决方案(嘘,不要告诉苹果)。但我想要一个没有的解决方案。
  • 请将您的代码添加到问题中。

标签: nstextview nsdocument


【解决方案1】:

我想出的代码如下:

#import <JRSwizzle/JRSwizzle.h>

@implementation NSDocument (Swizzle)

+ (void) load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSError * error = nil;

        SEL srcSelection = NSSelectorFromString(@"_checkAutosavingThenUpdateChangeCount:");
        SEL dstSelector = @selector(MyCheckAutosavingThenUpdateChangeCount:);

        [[NSDocument class] jr_swizzleMethod: srcSelection
                                      withMethod: dstSelector
                                           error: &error];
        NSLog(@"Error: %@", error);
    });
} // End of load

- (void) MyCheckAutosavingThenUpdateChangeCount: (unsigned long long) arg1
{
    // On updateChangeCount, we are going to catch if the firstresponder is our query window.
    // If the first responder is, then we will not mark a change. This is for two reasons:
    // 1. This is an sqlite app. Editing the query editor dosen't actually update our
    // document.
    // 2. If we do mark the document as updated and the query editor marks a change, we get
    // a popup stating that the document cant be updated, needs to be duplicated, yada, yada.
    NSWindow * keyWindow = [[NSApplication sharedApplication] keyWindow];
    if(NULL != keyWindow)
    {
        NSResponder * responder = keyWindow.firstResponder;
        // My code uses a NSTextView subclass. For copy/paste I'm just entering NSTextView.
        if([responder isKindOfClass: NSClassFromString(@"NSTextView")])
        {
            return;
        }
    }

    [self MyCheckAutosavingThenUpdateChangeCount: arg1];
} // End of MyCheckAutosavingThenUpdateChangeCount

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 2021-07-19
    • 2012-03-04
    • 2013-04-21
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多