Swift:通过在窗口中拖放来打开文件 [英] Swift: Opening a file by drag-and-drop in window

查看:71
本文介绍了Swift:通过在窗口中拖放来打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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?

原则上,在我看来,这与Apple的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆