我在主xib (MainMenu.xib)中有一个小窗口,其中有一个NSImageView控件,用于OS 应用程序。这个视图控件有一个NSImageView子类,它应该接受用户带来的文件(拖n拖放)。由于我没有开发带有Objective的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;
}现在,不管用户拖放和拖放到拖放框中的文件数量,拖放框一次只能获得一个文件路径。所以我想知道的是如何让应用程序读取用户带来的所有多个文件。
谢谢,
发布于 2013-05-08 21:13:16
将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;
}发布于 2015-09-13 23:00:00
迅捷风格:
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
}https://stackoverflow.com/questions/16449959
复制相似问题