读取多个拖拽的文件

El *_*ato 3 macos objective-c nspasteboard osx-mountain-lion nsdragginginfo

我在主xib(MainMenu.xib)中有一个小窗口,带有一个用于OS X应用程序的NSImageView控件.这个视图控件有一个NSImageView子类,它应该接受用户带来的文件(拖放).由于我没有使用Objective-C开发Mac应用程序的经验,所以我一直在搜索,查看Apple的一些示例项目,并得到一些想法.好吧,为了简短起见,我刚刚复制了这里发布的代码.有用.太棒了......以下是简洁版.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

目前,下拉框仅一次获取一个文件路径,而不管用户拖放到下拉框中的文件数量.所以我想知道的是如何让应用程序读取用户带来的所有多个文件.

谢谢,

rde*_*mar 13

将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;
}
Run Code Online (Sandbox Code Playgroud)