4

I have a small window inside the main xib (MainMenu.xib) with an NSImageView control for an OS X application. This view control has an NSImageView subclass that is supposed to accept files that the user brings (drag n drop). Since I have no experience in developing a Mac application with Objective-C, I've searched around, checking out some sample projects from Apple, and got some idea. Well, to make the story short, I've just copied the code posted here. It works. Great... The following is a concise version.

- (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;
}

For now, the drop box only gets file paths one at a time regardless of the number of files the user drags and drops onto the drop box. So what I would like to know is how to get the application to read all multiple files the user brings.

Thank you,

4

2 回答 2

14

将您的 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;
}
于 2013-05-08T21:13:16.657 回答
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
}
于 2015-09-13T23:00:00.173 回答