Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NSOpenPanel-所有内容都已弃用?_Macos_Cocoa_Nsopenpanel - Fatal编程技术网

Macos NSOpenPanel-所有内容都已弃用?

Macos NSOpenPanel-所有内容都已弃用?,macos,cocoa,nsopenpanel,Macos,Cocoa,Nsopenpanel,我一直在试图让一个窗口出现,让那个人选择一个文件,最终我做到了。问题是,Xcode抱怨我使用的方法不推荐使用。我查看了,但“running panels”部分下的所有内容在Mac OS 10.6中都已被弃用。我现在应该使用另一个类吗?据我所知,您可以使用runModal方法,如下所示: NSOpenPanel *openPanel = [[NSOpenPanel alloc] init]; if ([openPanel runModal] == NSOKButton) { NSStri

我一直在试图让一个窗口出现,让那个人选择一个文件,最终我做到了。问题是,Xcode抱怨我使用的方法不推荐使用。我查看了,但“running panels”部分下的所有内容在Mac OS 10.6中都已被弃用。我现在应该使用另一个类吗?

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

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

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

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

下面是一段关于如何使用它的代码片段:

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

六年后我发现这个问题很有用,既然没有快速的答案,这里有一个快速的解决方案

您将发现两个示例,一个作为独立窗口,另一个作为图纸

Swift 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
        }
    }
}

@Cole找不到此方法的原因是它是由
NSSavePanel
实现的,它是
NSOpenPanel
的超类+1运行“保存”或“打开”面板的其他更好方法也是如此,包括将面板作为工作表运行的方法。在Xcode中,在此代码中,alt单击
filename
,告诉我从10.6起,
[openPanel filename]
已被弃用。替换为
[openPanel URL]
(在Guillaume的回答中使用)。NSOKButton已被弃用:替换为NSModalResponseOK。这看起来是10.10下使用的正确代码,但是,如果使用ARC,请去掉保留位和释放位。或者,如果不想单独打开对话框,也可以使用。因此,
.begin
是一个完成处理程序。对于未来的旁观者:您也可以这样做:
let response=panel.runModal();if response==NSApplication.ModalResponse.OK{/*使用panel.url*/}
也适用于
。取消