将字符串数据从我的 Cocoa 应用程序拖动到第三方 Cocoa 应用程序
我想从我的表格视图中拖动一行并将其放入 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!!"]]
- Using the 10.5 version of the Pasteboard identifier,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其添加到控制器类的
awakeFromNib
中:(假设您有一个
IBOutlet
连接到名为tableView
的 tableView)。请务必在awakeFromNib
或更高版本中执行此操作。 (例如,如果您尝试在控制器类的init
方法中执行此操作,则您的 nib 文件尚未完全加载,并且您的IBOutlet
将全部为nil
并且该消息不会产生任何效果)。默认情况下,大多数拖动操作将仅限于本地应用程序而不是所有应用程序。
forLocal:
参数指定您引用的是应用程序本地(同一应用程序内)的拖动操作,还是非本地(即所有应用程序)。注释掉的行基本上就是 tableView 已经执行的操作。Add this in your controller class's
awakeFromNib
:(This is assuming you have an
IBOutlet
hooked up to your tableView namedtableView
). Be sure to do this inawakeFromNib
or later. (For example, if you were to try to do this in your controller class'sinit
methods, your nib files wouldn't be fully loaded yet, and yourIBOutlet
's would all benil
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.