NSOpenPanel in Swift . How to open?

后端 未结 5 1064
迷失自我
迷失自我 2021-02-06 23:42

I have this Objective-C Code :

- (IBAction)selectFileButtonAction:(id)sender {

    //create open panel...
    NSOpenPanel* openPanel = [NSOpenPanel openPanel];         


        
5条回答
  •  -上瘾入骨i
    2021-02-07 00:14

    Swift 4 version:

    let openPanel = NSOpenPanel()
            openPanel.canChooseFiles = false
            openPanel.allowsMultipleSelection = false
            openPanel.canChooseDirectories = false
            openPanel.canCreateDirectories = false
            openPanel.title = "Select a folder"
    
            openPanel.beginSheetModal(for:self.view.window!) { (response) in
                if response.rawValue == NSFileHandlingPanelOKButton {
                    let selectedPath = openPanel.url!.path
                    // do whatever you what with the file path
                }
                openPanel.close()
            }
    

提交回复
热议问题