Swift中的NSOpenPanel.怎么开? [英] NSOpenPanel in Swift . How to open?

查看:449
本文介绍了Swift中的NSOpenPanel.怎么开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Objective-C代码:

I have this Objective-C Code :

- (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];
            }

        }
    }];

现在,我想快速编写相同的内容.这是我到目前为止所做的:

Now I want to write this the same thing in swift. Here is what I've done until now :

@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)
}

在这里我被卡住了. 感谢您的帮助.

and here I'm stuck. Thanks for help.

推荐答案

@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,但是如果您要我添加更多内容,请发表评论. :)

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. :)

这篇关于Swift中的NSOpenPanel.怎么开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆