[英]NSOpenPanel in Swift . How to open?
我有這個 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)
}
我被卡住了。 感謝幫助。
@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 應該沒問題,但如果您希望我添加更多,請發表評論。 :)
對於 Swift 4,響應的檢查應該是
if response == .OK {
...
}
我 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()
}
}
斯威夫特 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()
}
NSOpenPanel | 蘋果開發者文檔https://developer.apple.com/documentation/appkit/nsopenpanel
斯威夫特5
@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()
}
}
}
在 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)
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.