在 NSCollectionView 子类中接受拖动操作
我已经对 NSCollectionView 进行了子类化,并且正在尝试从 Finder 接收拖动的文件。我收到 draggingEntered:
并返回适当的值,但我从未收到 prepareForDragOperation:
(也没有收到该过程中之后的任何方法)。我在这里缺少什么明显的东西吗?
代码:
- (void)awakeFromNib
{
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSLog(@"entered"); //Happens
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType])
{
NSLog(@"copy"); //Happens
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
NSLog(@"prepare"); //Never happens
return YES;
}
I've subclassed NSCollectionView and I'm trying to receive dragged files from the Finder. I'm receiving draggingEntered:
and returning an appropriate value, but I'm never receiving prepareForDragOperation:
(nor any of the methods after that in the process). Is there something obvious I'm missing here?
Code:
- (void)awakeFromNib
{
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSLog(@"entered"); //Happens
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType])
{
NSLog(@"copy"); //Happens
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
NSLog(@"prepare"); //Never happens
return YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这已经很晚了,但我发现了问题:
NSCollectionView 默默地提供了一个不兼容的实现:
...并且 Apple 没有记录这一点。如果您只是实现该方法来重新调用draggingEntered方法,则一切正常,例如:(
我来到SO希望找到此自定义实现提供的“魔力”的解释,因为这也是......未记录的(谢谢,Apple!)。我猜它在管理 CollectionView 中的插入点方面做了一些聪明的事情?)。
更新:似乎特殊的魔力就在 NSCollectionView 的委托对象内部。由于某种原因,Xcode4 声称没有我的委托,但分配它构建并运行正常。查看那里的所有自定义/半记录的拖/放方法。
(或者只是按照我上面描述的那样进行并覆盖自定义行为,并实现一些有效且您可以理解的东西)
This is pretty late, but I found the problem:
NSCollectionView silently provides an incompatible implementation of:
...and Apple hasn't documented this. If you simply implement that method to re-invoke the draggingEntered method, everything works fine, e.g.:
(I came to SO hoping to find an explanation of what "magic" this custom implementation provides, since that too is ... undocumented (thanks, Apple!). I'm guessing it does something clever with managing an insertion-point within the CollectionView?).
UPDATE: it seems the special magic is inside the NSCollectionView's delegate object. For some reason, Xcode4 was claiming there was no delegate for me, but assigning it built and ran OK. Check out all the custom / semi-documented drag/drop methods there.
(or just do as I describe above and override the custom behaviour, and implement something that works and you can understand)
您可能想尝试 NSCollectionViewDelegate 协议
特别是前两个方法。
You might want to try these delegate methods from the NSCollectionViewDelegate Protocol
The first two methods in particular.
我不久前经历过这个。这对我来说似乎违反直觉,但我让它工作的唯一方法是将关联的滚动视图设置为放置目标。
I went through this a while ago. It seemed counterintuitive to me, but the only way I could get it to work was to set up the associated scroll view as the drop target.