【问题标题】:swift 2.0 equivalent for NSOpenPanelNSOpenPanel 的 swift 2.0 等价物
【发布时间】:2015-09-14 17:54:44
【问题描述】:

我找到了这个 swift 1.2 教程来打开一个面板。但它在 swift 2.0 中不起作用。

@IBAction func selectFile(sender: AnyObject) {
    var openPanel = NSOpenPanel()
    openPanel.title = "Select file"
    openPanel.beginWithCompletionHandler({(result:Int) in
        if (result = NSFILEHandlingPanelOKButton){
            print(openPanel.URL!)
        }
    })
}

我收到错误未解析标识符 NSOpenPanel,swift 2.0 等效项是什么?

我也尝试在 iOS 和 MacOS 下创建 Cocoa 类,但没有任何运气。

【问题讨论】:

  • import Cocoaimport AppKit?
  • @mattr 好电话我忘了说那些导入给我的消息“没有这样的模块”顺便说一句,用 xcode 显式创建一个可可类会给出相同的错误消息。我猜我的 xcode 安装出了点问题?

标签: cocoa swift2 nsopenpanel


【解决方案1】:

如果还没有,请尝试导入 AppKit:

import AppKit

你可以阅读上面的Apple Docs

【讨论】:

    【解决方案2】:

    作为奖励,带有弹出框的自定义视图..

    func chooseDestFolder()->URL?{
    
    
    //it's an OPEN... :)
    let dialog = NSOpenPanel()
    
    //dialog.title                   = "Choose destination folder"
    dialog.message                 = "Choose destination folder"
    dialog.showsResizeIndicator    = true
    dialog.showsHiddenFiles        = false
    dialog.canChooseDirectories    = true
    dialog.canChooseFiles          = false
    dialog.canCreateDirectories    = true
    dialog.allowsMultipleSelection = false
    dialog.allowedFileTypes        = [];
    
    let sv = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 40))
    
    let menu = NSPopUpButton(radioButtonWithTitle: "AAA", target: nil, action: nil)
    menu.frame = CGRect(x: 0, y: 10, width: 100, height: 36)
    menu.addItems(withTitles: ["JPG", "PDF", ])
    sv.addSubview(menu)
    
    
    dialog.accessoryView = sv
    dialog.accessoryView?.wantsLayer = true
    //dialog.accessoryView?.layer?.backgroundColor = NSColor.red.cgColor
    dialog.isAccessoryViewDisclosed = true
    if (dialog.runModal() == NSApplication.ModalResponse.OK) {
        let destUrl = dialog.url
        return destUrl
    } else {
        // User clicked on "Cancel"
        return nil
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2016-03-21
      • 2014-07-23
      • 2014-11-23
      • 2021-07-17
      相关资源
      最近更新 更多