奇怪的 NSPasteboard 和 TextEdit 交互

发布于 2024-10-02 07:59:26 字数 1907 浏览 4 评论 0原文

我正在从应用程序中的两个位置拖放字符串。一种来自自定义 NSView,另一种来自 NSTableDataSource 委托。第一个允许我将文本拖动到 TextEdit 应用程序中,第二个则不允许。我可以从这两个应用程序拖动到其他应用程序,例如 Terminal 或 TextMate。据我所知,我对两者使用几乎相同的代码。

我正在尝试找出问题所在。它原则上有效,因为我可以拖动到某些应用程序。但是 TextEdit 发生了什么?

从自定义 NSView:

(void)mouseDown:(NSEvent *)theEvent
{
    NSString *testString = @"TEST";

    NSImage *dragImage;
    NSPoint dragPosition;

    NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];

    [pboard declareTypes:[NSArray arrayWithObjects: NSStringPboardType, nil] owner:nil];
    [pboard setString:testString forType:NSStringPboardType];

    dragImage = // blah
    dragPosition = [self convertPoint:[theEvent locationInWindow] fromView:nil];

    [self dragImage:dragImage
                 at:dragPosition
             offset:NSZeroSize
              event:theEvent
         pasteboard:pboard
             source:self
          slideBack:YES];
    }
}

从表委托:

(BOOL)tableView: (NSTableView *)aTableView writeRowsWithIndexes: (NSIndexSet *)indexes toPasteboard: (NSPasteboard *)pboard
{
    NSString *testString = @"TEST";

    [pboard declareTypes:[NSArray arrayWithObjects: NSStringPboardType, nil] owner:nil];
    [pboard setString:testString forType:NSStringPboardType];

    return YES;
}

如果我打印出第一个的 pboard 类型:

"public.utf8-plain-text",
NSStringPboardType,

第二个:

"public.utf8-plain-text",
NSStringPboardType,

同样,第一个的内容:

2010-11-16 13:56:01.832 XXX[1654:a0f] public.utf8-plain-text:TEST
2010-11-16 13:56:01.838 XXX[1654:a0f] NSStringPboardType:TEST

第二个

2010-11-16 13:56:05.623 XXX[1654:a0f] public.utf8-plain-text:TEST
2010-11-16 13:56:05.623 XXX[1654:a0f] NSStringPboardType:TEST

据我所知,两者的行为应该相同相同。但他们不是。有想法吗?

I am drag-dropping a string from two places in my app. One from a custom NSView and one from a NSTableDataSource delegate. The first one allows me to drag text into the TextEdit application, the second one does not. I can drag from both to other apps such as Terminal or TextMate. As far as I can tell, I'm using close to identical code for both.

I'm trying to figure out what's wrong. It's working in principle because I can drag to some apps. But what's going on with TextEdit?

From the custom NSView:

(void)mouseDown:(NSEvent *)theEvent
{
    NSString *testString = @"TEST";

    NSImage *dragImage;
    NSPoint dragPosition;

    NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];

    [pboard declareTypes:[NSArray arrayWithObjects: NSStringPboardType, nil] owner:nil];
    [pboard setString:testString forType:NSStringPboardType];

    dragImage = // blah
    dragPosition = [self convertPoint:[theEvent locationInWindow] fromView:nil];

    [self dragImage:dragImage
                 at:dragPosition
             offset:NSZeroSize
              event:theEvent
         pasteboard:pboard
             source:self
          slideBack:YES];
    }
}

From the table delegate:

(BOOL)tableView: (NSTableView *)aTableView writeRowsWithIndexes: (NSIndexSet *)indexes toPasteboard: (NSPasteboard *)pboard
{
    NSString *testString = @"TEST";

    [pboard declareTypes:[NSArray arrayWithObjects: NSStringPboardType, nil] owner:nil];
    [pboard setString:testString forType:NSStringPboardType];

    return YES;
}

If I print out the pboard's types for the first one:

"public.utf8-plain-text",
NSStringPboardType,

And for the second:

"public.utf8-plain-text",
NSStringPboardType,

Likewise, the content for the first:

2010-11-16 13:56:01.832 XXX[1654:a0f] public.utf8-plain-text:TEST
2010-11-16 13:56:01.838 XXX[1654:a0f] NSStringPboardType:TEST

And second

2010-11-16 13:56:05.623 XXX[1654:a0f] public.utf8-plain-text:TEST
2010-11-16 13:56:05.623 XXX[1654:a0f] NSStringPboardType:TEST

So as far as I can tell, the two should be behaving identically. But they're not. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文