2012-08-28 4 views
2

"간단한"드래그 내 Mac 응용 프로그램에 & 드래그를 구현하고 싶습니다. 뷰에서 드래그하여 그에 따라 nib 파일에 "MyView"를 입력했습니다. 그러나 콘솔에서 응답을받지 못했습니다 (프로토콜 메서드가 트리거 될 때마다 메시지를 기록하려고합니다)코코아의 드래그 앤 드롭 (<NSDraggingDestination>)이 작동하지 않습니다.

NSView를 서브 클래 싱했습니다 이 같은 :

@interface MyView : NSView <NSDraggingDestination>{ 
    NSImageView* imageView; 
} 

과 같이 그것을 구현 :

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     [self registerForDraggedTypes:[NSImage imagePasteboardTypes]]; 

     NSRect rect = NSMakeRect(150, 0, 400, 300); 
     imageView = [[NSImageView alloc] initWithFrame:rect]; 

     [imageView setImageScaling:NSScaleNone]; 
     [imageView setImage:[NSImage imageNamed:@"test.png"]]; 
     [self addSubview:imageView]; 



    } 

    return self; 
} 


- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{ 
    NSLog(@"entered"); 
    return NSDragOperationCopy; 

} 

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender{ 
    return NSDragOperationCopy; 

} 


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

} 
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{ 
    NSLog(@"som"); 
    return YES; 

} 

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { 

    NSPasteboard *pboard = [sender draggingPasteboard]; 

    if ([[pboard types] containsObject:NSURLPboardType]) { 
     NSURL *fileURL = [NSURL URLFromPasteboard:pboard]; 
     NSLog(@"%@", fileURL); 
    } 
    return YES; 
} 

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender{ 
    NSLog(@"conclude sth"); 


} 

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


} 

- (BOOL)wantsPeriodicDraggingUpdates{ 
    NSLog(@"wants updates"); 

} 


- (void)updateDraggingItemsForDrag:(id <NSDraggingInfo>)sender NS_AVAILABLE_MAC(10_7){ 
} 

답변

11

사람이 여전히 사용이 필요한 경우 :

대신은 :

[self registerForDraggedTypes: 
         [NSImage imagePasteboardTypes]]; // BAD: dropped image runs away, no draggingdestination protocol methods sent... 

나를 위해이 문제를 해결했다. 내 XIB는 osX 10.5를 대상으로하기 때문에이 기능이 작동하는 것으로 판단됩니다. NSFilenamesPboardType은 10.5 및 이전 '표준 데이터'UTI를 나타내며 [NSImage imagePasteboardTypes]는 10.6 이후의 표준 데이터 UTI를 반환합니다. 의 방법으로

, - (BOOL) performDragOperation (ID NSDraggingInfo) 보낸 사람
, 당신은으로 떨어 된 파일의 경로를 얻을 수 있습니다 :

NSArray *files = [pboard propertyListForType:NSFilenamesPboardType]; 

여기에 솔루션을 얻었다을 :

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DragandDrop/Tasks/acceptingdrags.html#//apple_ref/doc/uid/20000993-BABHHIHC

+0

imagePasteboardTypes = 이미지 콘텐츠 만 –