2011-10-07 69 views
11

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

回答

25

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

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

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

@Cole的,你就不会发现这个方法的原因是因为它是由'NSSavePanel',这是'NSOpenPanel'的超类实现。 +1 – ughoavgfhw

+0

啊,谢谢Jesse和@​​ughoavgfhw。 – Cole

+1

运行保存或打开面板的其他更好的方法也是如此,包括将其中一个作为工作表运行的方法。 –

29

在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]; 
}]; 
+2

这看起来像是在10.10下使用的正确代码,但是如果您使用ARC,则需要删除保留位和释放位。 – smacdonald

+0

或者,您可以使用[ - beginSheetModalForWindow:completionHandler:](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSavePanel_Class/#//apple_ref/occ/instm/NSSavePanel/ beginSheetModalForWindow:completionHandler :)如果你不想单独打开对话框。 – pi3

3

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

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

雨燕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 
     } 
    } 
} 
+0

所以'.begin'是一个完成处理程序。对于将来的在线用户:你也可以这样做:'let response = panel.runModal(); if response == NSApplication.ModalResponse.OK {/ *用panel.url * /}做事''也适用于'.CANCEL' – eonist

相关问题