我有这个Objective-C代码:
- (IBAction)selectFileButtonAction:(id)sender {
//create open panel...
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
// NSLog(@"Open Panel");
//set restrictions / allowances...
[openPanel setAllowsMultipleSelection: NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanCreateDirectories:NO];
[openPanel setCanChooseFiles:YES];
//only allow images...
[openPanel setAllowedFileTypes:[NSImage imageFileTypes]];
//open panel as sheet on main window...
[openPanel beginWithCompletionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
//get url (should only be one due to restrictions)...
for( NSURL* URL in [openPanel URLs] ) {
// self.roundClockView1.URL = URL ;
_thePath = URL;
currentSelectedFileName = [[URL path] lastPathComponent];
// [_roundClockView1 setNeedsDisplay:1];
[self openEditor];
}
}
}];
现在我想在swift中写同样的东西。以下是我迄今为止所做的事情:
@IBAction func selectAnImageFromFile(sender: AnyObject) {
var openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = true
openPanel.beginWithCompletionHandler(handler: (Int) -> Void)
}
在这里,我被困住了。 谢谢你的帮助。
答案 0 :(得分:37)
@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
}
}
}
我猜你被困在完成处理程序部分?在任何情况下,从开放面板处理URL应该可以,但如果您想要我添加更多,请发表评论。 :)
答案 1 :(得分:6)
对于Swift 4,应该检查响应
if response == .OK {
...
}
答案 2 :(得分:4)
我的2美分,速成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()
}
}
答案 3 :(得分:3)
Swift 4版本:
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()
}
答案 4 :(得分:1)
NSOpenPanel |苹果开发者文档 https://developer.apple.com/documentation/appkit/nsopenpanel
Swift5
@objc func changeConfigFolder() {
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = false
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = true
openPanel.title = NSLocalizedString("change_the_folder", comment: "")
openPanel.begin { [weak self] (result) -> Void in
if result == .OK {
let selectedPath = openPanel.url!.path
if var userSetting = self?.userSetting {
userSetting["save_path"] = selectedPath
UserDefaults.standard.set(userSetting, forKey: "config")
self?.resetStatusBar()
}
} else {
openPanel.close()
}
}
}
答案 5 :(得分:0)
在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)
}
}
}
}
}