macos NSOpenPanel - 一切都被弃用了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7693896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:29:59  来源:igfitidea点击:

NSOpenPanel - Everything deprecated?

macoscocoansopenpanel

提问by Cole

I've been trying to get a window to show up asking the person to choose a file, and I eventually did. The problem is, Xcode complains that the method I'm using is deprecated. I looked in the class reference, but everything under the "running panels" section has been deprecated as of Mac OS 10.6. Is there a different class I'm supposed to be using now?

我一直试图让一个窗口出现,要求该人选择一个文件,我最终做到了。问题是,Xcode 抱怨我使用的方法已被弃用。我查看了类参考,但从 Mac OS 10.6 开始,“运行面板”部分下的所有内容都已弃用。我现在应该使用不同的课程吗?

回答by Guillaume

In 10.6, there was a few changes to this classes. One of the benefits is that there is now a block-based API.

在 10.6 中,对此类进行了一些更改。好处之一是现在有一个基于块的 API。

Here is a code snippet on how to use that:

这是有关如何使用它的代码片段:

NSOpenPanel *panel = [[NSOpenPanel openPanel] retain];

// Configure your panel the way you want it
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:NO];
[panel setAllowsMultipleSelection:YES];
[panel setAllowedFileTypes:[NSArray arrayWithObject:@"txt"]];

[panel beginWithCompletionHandler:^(NSInteger result){
    if (result == NSFileHandlingPanelOKButton) {

        for (NSURL *fileURL in [panel URLs]) {
            // Do what you want with fileURL
            // ...
        }
    }

    [panel release];
}];

回答by Jesse Dunlap

As far as I know, you can use the runModalmethod like shown below:

据我所知,您可以使用runModal如下所示的方法:

NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];

if ([openPanel runModal] == NSOKButton)
{
    NSString *selectedFileName = [openPanel filename];
}

回答by digitalHound

Seeing how I found this question useful six years later, and since there are no swift answers, here's a swift solution.

六年后看到我如何发现这个问题很有用,并且由于没有快速的答案,这里有一个快速的解决方案。

You'll find two samples, one as a stand alone window and the other as a sheet.

您会发现两个样本,一个作为独立窗口,另一个作为工作表。

Swift 3.0

斯威夫特 3.0

func selectIcon() {
    // create panel
    let panel = NSOpenPanel()

    // configure as desired
    panel.canChooseFiles = true
    panel.canChooseDirectories = false
    panel.allowsMultipleSelection = false
    panel.allowedFileTypes = ["png"]

    // *** ONLY USE ONE OF THE FOLLOWING OPTIONS, NOT BOTH ***

    // ********************** OPTION 1 ***********************
    // use this if you want a selection window to display that is
    // displayed as a separate stand alone window
    panel.begin { [weak self] (result) in
        guard result == NSFileHandlingPanelOKButton, panel.urls.isEmpty == false, let url = panel.urls.first else {
            return
        }

        let image = NSImage.init(contentsOf: url)
        DispatchQueue.main.async {
            self?.iconImageView.image = image
        }
    }

    // ********************** OPTION 2 ***********************        
    // use this if you want a sheet style view that displays sliding
    // down from your apps window
    panel.beginSheetModal(for: self.view.window!) { [weak self] (result) in
        guard result == NSFileHandlingPanelOKButton, panel.urls.isEmpty == false, let url = panel.urls.first else {
            return
        }

        let image = NSImage.init(contentsOf: url)
        DispatchQueue.main.async {
            self?.iconImageView.image = image
        }
    }
}