拖拽drop in cocoa app不适用于NSView的子类

时间:2014-12-16 01:34:00

标签: objective-c cocoa

我正在尝试按照本教程(https://www.youtube.com/watch?v=UZ0mp3-JuzY&index=25&list=PLE83F832121568D36)进行拖动和放大放入Cocoa app。

我正在使用XCode6,因此视频中使用的某些方法已被弃用。

我已经创建了NSView的子类,名为DropView,我将自己的customView标识符设为DropView

以下是我现在的代码:

#import "DropView.h"
@implementation DropView

-(id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if(self) {
        [self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
    }
    return self;
}


-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender
{
    if ([NSImage canInitWithPasteboard:[sender draggingPasteboard]] && [sender draggingSourceOperationMask] & NSDragOperationCopy)
    {
        NSLog(@"ENTERED");
        return NSDragOperationCopy;
    }
    NSLog(@"NEVER ENTERED IN DRAGGING ENTERED METHOD");
    return NSDragOperationNone;
}


-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
{
    NSLog(@"DRAGGING");
    return NSDragOperationCopy;
}


-(void)draggingEnded:(id<NSDraggingInfo>)sender
{
    NSLog(@"ENDED!");
}


-(void)draggingExited:(id<NSDraggingInfo>)sender
{
    NSLog(@"EXITED!");
}


-(BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender
{
    NSLog(@"DID I EVER ENTER PREPARE FOR DRAG OPERATION METHOD?");
    return YES;
}


-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender
{
    if([NSImage canInitWithPasteboard:[sender draggingPasteboard]])
    {
        NSImage *newImage = [[NSImage alloc ]initWithPasteboard:[sender draggingPasteboard]];
        [self setImage:newImage];
    }
    return YES;
}


-(void)concludeDragOperation:(id<NSDraggingInfo>)sender
{
    [self setNeedsDisplay:YES];
}

@end

我没有收到任何错误,但似乎没有执行任何代码。我的猜测是frameRect是nil,即没有初始化,但我不明白为什么。有人可以帮我解决这个问题吗?

问候,约翰

2 个答案:

答案 0 :(得分:0)

好的,解决方案是将DropView作为NSBox的子类,而不是NSView,即使我不明白为什么......

答案 1 :(得分:0)

是-initWithFrame:叫吗?

如果您是从IB创建视图,请注册拖放-initWithCoder:或-awakeFromNib