NSOpenPanel in Swift . How to open?

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

I have this Objective-C Code :

- (IBAction)selectFileButtonAction:(id)sender {

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


        
相关标签:
5条回答
  • 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()
            }
    
    0 讨论(0)
  • 2021-02-07 00:17
    @IBAction func selectAnImageFromFile(sender: AnyObject) {
        let openPanel = NSOpenPanel()
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = false
        openPanel.canCreateDirectories = false
        openPanel.canChooseFiles = true
        openPanel.beginWithCompletionHandler { (result) -> Void in
            if result == NSFileHandlingPanelOKButton {
                //Do what you will
                //If there's only one URL, surely 'openPanel.URL'
                //but otherwise a for loop works
            }
        }
    }
    

    I'm guessing you got stuck on the completion handler part? In any case, handling the URL from the open panel should be ok from there, but comment if you want me to add more. :)

    0 讨论(0)
  • For Swift 4 the check of the response should be

    if response == .OK { 
      ... 
    }
    
    0 讨论(0)
  • 2021-02-07 00:29

    my two cents for swift 5.0

    override func showChooseFileDialog(title: String){
        let openPanel = NSOpenPanel()
        openPanel.canChooseFiles = false
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = false
        openPanel.canCreateDirectories = false
        openPanel.title = title
    
        openPanel.beginSheetModal(for:self.view.window!) { (response) in
            if response == .OK {
                let selectedPath = openPanel.url!.path
                // do whatever you what with the file path
            }
            openPanel.close()
        }
    }
    
    0 讨论(0)
  • 2021-02-07 00:34

    In SwiftUI

        struct FileView: View {
        var body: some View {
            Button("Press Me") {
                let openPanel = NSOpenPanel()
                openPanel.prompt = "Select File"
                openPanel.allowsMultipleSelection = false
                    openPanel.canChooseDirectories = false
                    openPanel.canCreateDirectories = false
                    openPanel.canChooseFiles = true
                    openPanel.begin { (result) -> Void in
                        if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
                            let selectedPath = openPanel.url!.path
                            print(selectedPath)
    
                        }
                    }
               }
            }
         }
    
    0 讨论(0)
提交回复
热议问题