1

NSImageView 컨트롤이 OS X 애플리케이션 인 메인 xib (MainMenu.xib) 안에 작은 창이 있습니다. 이 뷰 컨트롤에는 사용자가 가져 오는 파일을 받아들이도록되어있는 NSImageView 하위 클래스가 있습니다 (드래그 앤 드롭). Objective-C로 Mac 응용 프로그램을 개발 한 경험이 없기 때문에 Apple의 샘플 프로젝트를 살펴보고 몇 가지 아이디어를 얻었습니다. 이야기를 짧게 만들기 위해서, 나는 방금 here이라는 코드를 복사했습니다. 그것은 작동합니다. 위대한 ... 다음은 간결한 버전입니다.여러 드래그 앤 드롭 파일 읽기

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

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender{ 
} 

- (void)draggingExited:(id <NSDraggingInfo>)sender{ 
} 

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

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { 
    NSPasteboard *pboard = [sender draggingPasteboard]; 
    if ([[pboard types] containsObject:NSURLPboardType]) { 
     NSURL *fileURL = [NSURL URLFromPasteboard:pboard]; 
     NSLog(@"Path: %@", [self convertPath:fileURL]); // <== That's just what I need 
    } 
    return YES; 
} 

- (NSString *)convertPath:(NSURL *)url { 
    return url.path; 
} 

현재 드롭 상자는 사용자가 끌어다 놓기 상자에 끌어다 놓은 파일 수에 관계없이 한 번에 하나씩 파일 경로 만 가져옵니다. 그래서 내가 알고 싶은 것은 응용 프로그램이 사용자가 가져 오는 여러 파일을 모두 읽는 방법입니다.

답변

11
은 performDragOperation 변경

, 감사 : 방법이에 :

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { 
    NSPasteboard *pboard = [sender draggingPasteboard]; 
    if ([[pboard types] containsObject:NSURLPboardType]) { 
     NSArray *urls = [pboard readObjectsForClasses:@[[NSURL class]] options:nil]; 
     NSLog(@"URLs are: %@", urls); 
    } 
    return YES; 
} 
+0

가 대단히 감사합니다. 그것은 작동합니다. 다음 주제가 나를 도울 수있는 것 같습니다. http://stackoverflow.com/questions/1998158/how-do-i-handle-multiple-file-drag-drop-from-finder-in-mac-os-x-10-5?rq=1 –

+0

이 답변 황금이다. 내 페이스트 보드에서 하나 이상의 NSURL을 수신하는 데 막대한 어려움을 겪고있었습니다. 나는 항상 한 명으로 제한되었다. 이 문제가 해결되었습니다. –

2

스위프트 스타일 :

override func performDragOperation(sender: NSDraggingInfo) -> Bool 
{ 
    if let board = sender.draggingPasteboard().propertyListForType(NSFilenamesPboardType) as? NSArray 
    {    
     for imagePath in board 
     { 
      if let path = imagePath as? String 
      { 
       println("path: \(path)") 
      } 
     }     
     return true    
    } 
    return false 
}