简体   繁体   English

Objective-C 目录选择器 (OSX 10.7)

[英]Objective-C Directory Picker ( OSX 10.7 )

So I currently have this bit of code to get a dir:所以我目前有这段代码来获取目录:

-(NSString *)get {      
  NSOpenPanel *gitDir = [NSOpenPanel openPanel];
  NSInteger *ger = [gitDir runModalForTypes:nil];
  NSString *Directory = [gitDir directory];
  return Directory;
}

But it gives me errors and says it has now been depreciated.但它给了我错误并说它现在已经折旧了。

Is there a better way for OSX 10.7? OSX 10.7 有更好的方法吗?

Whenever you see a deprecation warning you should go straight to the official documentation.每当您看到弃用警告时,您应该直接查看官方文档。 In this case, the docs for NSOpenPanel say:在这种情况下, NSOpenPanel 的文档说:

runModalForTypes: Displays the panel and begins a modal event loop that is terminated when the user clicks either OK or Cancel. runModalForTypes:显示面板并开始模态事件循环,当用户单击“确定”或“取消”时终止该循环。 (Deprecated in Mac OS X v10.6. Use runModal instead. You can set fileTypes using setAllowedFileTypes:.) (在 Mac OS X v10.6 中已弃用。请改用 runModal。您可以使用 setAllowedFileTypes: 设置文件类型。)

This is a supplement to sosborn's answer, not a replacement.这是对索斯伯恩答案的补充,而不是替代。

runModalForTypes: is deprecated, and the correct replacement is runModal (or setAllowedFileTypes: followed by runModal , but in this case you're passing nil for the types). runModalForTypes:已弃用,正确的替换是runModal (或setAllowedFileTypes:后跟runModal ,但在这种情况下,您为类型传递 nil )。

directory is also deprecated, and the correct replacement is directoryURL . directory也已弃用,正确的替换是directoryURL (If you actually must return an NSString path rather than an NSURL , just return [[gitDir directoryURL] path] .) (如果您实际上必须返回NSString路径而不是NSURL ,只需返回[[gitDir directoryURL] path] 。)

However, what you're doing is asking the user to select a file, and then returning the directory that file is in, when what you really want is to ask the user to select a directory.但是,您正在做的是要求用户选择一个文件,然后返回该文件所在的目录,而您真正想要的是要求用户选择一个目录。 To do that, you want to call setCanChooseFiles to NO and setCanChooseDirectories to YES , and then call URLs to get the directory the user selected.为此,您需要将setCanChooseFiles调用为NO并将setCanChooseDirectoriesYES ,然后调用 URL 以获取用户选择的目录。

Also, you're ignoring the result of runModal (or runModalForTypes: ).此外,您忽略了runModal (或runModalForTypes: )的结果。 I'm sure the compiler is warning you about the unused variable "ger", and you shouldn't just ignore warnings.我确信编译器会警告您未使用的变量“ger”,您不应该只是忽略警告。 If the user cancels the panel, you're going to treat that as clicking OK, and select whatever directory she happened to be in when she canceled.如果用户取消面板,您会将其视为单击“确定”,并选择她取消时碰巧所在的目录。

Here's a better implementation, which will return the URL of the selected directory, or nil if the user canceled (or somehow managed to not select anything).这是一个更好的实现,它将返回所选目录的 URL,如果用户取消(或以某种方式设法不选择任何内容),则返回 nil。 Again, if you need an NSString , just add a "path" call to the return statement:同样,如果您需要NSString ,只需在 return 语句中添加一个“路径”调用:

-(NSURL *)get {      
   NSOpenPanel *panel = [NSOpenPanel openPanel];
   [panel setAllowsMultipleSelection:NO];
   [panel setCanChooseDirectories:YES];
   [panel setCanChooseFiles:NO];
   if ([panel runModal] != NSModalResponseOK) return nil;
   return [[panel URLs] lastObject];
}

I adapted the code by abarnert for swift.我为 swift 修改了 abarnert 的代码。 tx for the code just what I needed. tx 代码正是我需要的。

func askUserForDirectory() -> NSURL? {
        let myPanel:NSOpenPanel = NSOpenPanel()
        myPanel.allowsMultipleSelection = false
        myPanel.canChooseDirectories = true
        myPanel.canChooseFiles = false
        if ( myPanel.runModal() != NSFileHandlingPanelOKButton ) {
            return nil
        }
        return myPanel.URLs[0] as? NSURL
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM