2011-10-07 67 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

相關問題