将字符串数据从我的 Cocoa 应用程序拖动到第三方 Cocoa 应用程序

发布于 2024-10-08 19:30:49 字数 963 浏览 0 评论 0原文

我想从我的表格视图中拖动一行并将其放入 Mac OS X 10.6 中的任何其他 NSTextField 中,并放置一串文本。

拖放已经可以在我的应用程序中使用(在 NSTableView 和 NSBrowser 之间),但我没有成功地将任何数据放置在源应用程序以外的应用程序可以接受的粘贴板上。

这是我尝试过的代码,我认为当我放入其他 NSTextField 时,这足以让单词“hello”被“粘贴”:

-(BOOL) tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes     toPasteboard:(NSPasteboard *)pboard {

    [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
    [pboard setString:@"hello" forType:NSStringPboardType];

return YES;

}

//--

我从来没有得到显示我将接受放置的光标,但它就是行不通。

  • 我尝试过的事情:
    • 使用 10.5 版本的粘贴板标识符, NSStringPBoardType
    • 使用10.6版本,NSPasteboardTypeString。
    • 设置所有者 = nil,因为我不懒惰地提供数据。
    • 使用键控存档器:[pboard setData:[NSKeyedArchiver archivedRootObject:@"Hello!!"]]

以上均无效。我认为我的概念是正确的:“编码数据,告诉粘贴板你有什么,然后给它数据”,但由于其他应用程序无法识别它,我怀疑我没有告诉粘贴板正确的数据类型。

我哪里出错了?

谢谢, 伍迪

I want to drag a row from my tableview and drop it into any other NSTextField in Mac OS X 10.6, and have a string of text be dropped.

Drag and drop already works within my app (between a NSTableView and an NSBrowser), but I have had no success putting any data on the pasteboard that can accepted by apps other than the source application.

Here's the code I tried, which I thought would be enough to get hte word "hello" to be 'pasted' when I drop into some other NSTextField:

-(BOOL) tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes     toPasteboard:(NSPasteboard *)pboard {

    [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
    [pboard setString:@"hello" forType:NSStringPboardType];

return YES;

}

//--

I never get the cursor that shows me the drop will be accepted, and it just doesn't work.

  • Things I've tried:
    • Using the 10.5 version of the Pasteboard identifier,
      NSStringPBoardType
    • Using the 10.6 version, NSPasteboardTypeString.
    • Setting the owner = nil, since I'm not providing the data lazily.
    • Using the keyed archiver: [pboard setData:[NSKeyedArchiver archivedRootObject:@"Hello!!"]]

None of the above have worked. I think I have the concepts correct: "Encode data, tell the pasteboard what you've got, then give it the data", but since other apps don't recognize it, I suspect I'm not telling the pasteboard the correct datatype.

Where am I going wrong?

Thanks,
Woody

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2024-10-15 19:30:49

将其添加到控制器类的 awakeFromNib 中:(

- (void)awakeFromNib {
    [tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
    // [tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];

}

假设您有一个 IBOutlet 连接到名为 tableView 的 tableView)。请务必在 awakeFromNib 或更高版本中执行此操作。 (例如,如果您尝试在控制器类的 init 方法中执行此操作,则您的 nib 文件尚未完全加载,并且您的 IBOutlet 将全部为 nil 并且该消息不会产生任何效果)。

默认情况下,大多数拖动操作将仅限于本地应用程序而不是所有应用程序。 forLocal: 参数指定您引用的是应用程序本地(同一应用程序内)的拖动操作,还是非本地(即所有应用程序)。注释掉的行基本上就是 tableView 已经执行的操作。

Add this in your controller class's awakeFromNib:

- (void)awakeFromNib {
    [tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
    // [tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];

}

(This is assuming you have an IBOutlet hooked up to your tableView named tableView). Be sure to do this in awakeFromNib or later. (For example, if you were to try to do this in your controller class's init methods, your nib files wouldn't be fully loaded yet, and your IBOutlet's would all be nil and the message would have no effect).

By default, most drag operations will be limited to the local application rather than all applications. The forLocal: parameter specifies whether you are referring to drag operations that are local to the application (within the same app), or non-local, meaning all applications. The line that's commented out is basically what you already have the tableView doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文