Swift:通过在窗口中拖放来打开文件 [英] Swift: Opening a file by drag-and-drop in window
问题描述
在Swift中,如何在Mac应用程序的窗口中建立一个区域,用户可以在其中将文件夹拖放到该区域,并让我的应用程序接收文件夹的路径?
In Swift, how can I build an area in a window of my Mac app where a user can drag-and-drop a folder onto this area, and have my app receive the path of the folder?
In principle, it seems to me that this is a similar concept to Apple's CocoaDragAndDrop example app. I've tried to work my way through understanding that source code, but the app is written in Objective-C and I've not successfully managed to replicate its functionality in the app I am building.
谢谢.
推荐答案
在自定义NSView中:
In a custom NSView:
override init(frame frameRect: NSRect)
{
super.init(frame: frameRect)
registerForDraggedTypes([kUTTypeFileURL,kUTTypeImage])
}
override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation
{
println("dragging entered")
return NSDragOperation.Copy
}
问题是,要使其正常工作,我必须将自定义视图作为叶子,并将最后一个视图置于情节提要中
The catch is, to get it working I had to put that custom view as a leaf and the last view in the storyboard
这篇关于Swift:通过在窗口中拖放来打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!