Swift: Opening a file by drag-and-drop in window

后端 未结 3 2013
傲寒
傲寒 2021-02-06 10:49

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 pr

3条回答
  •  星月不相逢
    2021-02-06 11:26

    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

提交回复
热议问题