NSOpenPanel in Swift . How to open?

后端 未结 5 1128
迷失自我
迷失自我 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: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)
    
                        }
                    }
               }
            }
         }
    

提交回复
热议问题