拖放不适用于NSBox的子类

时间:2009-09-17 00:34:03

标签: objective-c cocoa drag-and-drop subclass nsdragginginfo

我已经创建了一个NSBox的子类来实现拖放。我有以下代码:

@interface DropView : NSBox {

}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
@end

@implementation DropView
- (void)awakeFromNib
{
    [self registerForDraggedTypes:
  [NSArray arrayWithObject: NSFilenamesPboardType]];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
 NSDragOperation sourceDragMask = [sender 
           draggingSourceOperationMask];
 if (sourceDragMask & NSDragOperationLink) {
  return NSDragOperationLink;
 } else if (sourceDragMask & NSDragOperationCopy) {
  return NSDragOperationCopy;
 }
 return NSDragOperationNone;
}

-(BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
 NSPasteboard *pboard=[sender draggingPasteboard];
 NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
 NSEnumerator *e=[files objectEnumerator];
 NSString *str=nil;
 while(str=[e nextObject]) {
  NSLog(@"Got %@\n", str);
 }

 return (TRUE);
}
@end

但是,拖放不起作用。当我尝试将某些东西拖入盒子时,我看不到小绿色加号。

由于

1 个答案:

答案 0 :(得分:3)

修正了问题。不是将NSView的类设置为DropView,而是将NSBox的类设置为DropView,效果很好: - )