首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSOpenPanel在斯威夫特。怎么打开?

NSOpenPanel在斯威夫特。怎么打开?
EN

Stack Overflow用户
提问于 2014-10-28 13:35:59
回答 5查看 15K关注 0票数 27

我有一个目标-C代码:

代码语言:javascript
复制
- (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];
            }

        }
    }];

现在我想用斯威夫特写同样的东西。以下是我到现在为止所做的事情:

代码语言:javascript
复制
@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)
}

我被困在这里了。谢谢你帮忙。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-10-28 15:35:56

代码语言:javascript
复制
@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应该是可以的,但是如果您希望我添加更多内容,请进行注释。:)

票数 48
EN

Stack Overflow用户

发布于 2019-08-07 09:58:50

我的两分钱换成了斯威夫特5.0

代码语言:javascript
复制
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()
    }
}
票数 7
EN

Stack Overflow用户

发布于 2018-01-17 11:08:57

Swift 4版本:

代码语言:javascript
复制
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()
        }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26609778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档