Mac app开发跟ios开发有点区别,网上教程比较少
这个例子是,点击按钮以后,打开选择文件夹的窗口,可以多选文件夹,另一个按钮可以多选文件.然后打印出目录
运行效果
NSOpenPanel讲解
首先把2个按钮连线到swift文件中,2个按钮代码如下: NSOpenPanel 创建对象以后通过 设置属性canChooseDirectories和canChooseFiles实现打开文件或者文件夹 通过block beginSheetModal实现打开文件窗口.
代码例子
var pathUrls = [URL]()
@IBAction func btnOpenFileClick(_ sender: Any) {
let openPanel = NSOpenPanel()
openPanel.prompt = "选择"
openPanel.canChooseDirectories = false
openPanel.canChooseFiles = true
openPanel.allowsMultipleSelection = true
openPanel.beginSheetModal(for: view.window!) {[unowned self] result in
if result != .OK {
print("点击取消")
return
}
for url in openPanel.urls{
print("路径是=",url.path)
}
pathUrls = openPanel.urls
}
}
@IBAction func btnOpenDirClick(_ sender: Any) {
let openPanel = NSOpenPanel()
openPanel.prompt = "选择"
openPanel.canChooseDirectories = true
openPanel.allowsMultipleSelection = true
openPanel.canChooseFiles = false
openPanel.beginSheetModal(for: view.window!) { [unowned self] result in
if result != .OK {
print("点击取消")
return
}
for url in openPanel.urls{
print("路径是=",url.path)
}
pathUrls = openPanel.urls
}
}
@IBAction func btnOpenPathClick(_ sender: Any) {
print("pathUrls=",pathUrls)
NSWorkspace.shared.activateFileViewerSelecting(pathUrls)
}
|