Col*_*ole 12 macos cocoa nsopenpanel
我一直试图让一个窗口显示要求该人选择一个文件,我最终做到了.问题是,Xcode抱怨我正在使用的方法已被弃用.我查看了类引用,但从Mac OS 10.6开始,"运行面板"部分下的所有内容都已弃用.我现在应该使用不同的课吗?
Gui*_*ume 30
在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];
}];
Run Code Online (Sandbox Code Playgroud)
Jes*_*lap 27
据我所知,您可以使用runModal
如下所示的方法:
NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
if ([openPanel runModal] == NSOKButton)
{
NSString *selectedFileName = [openPanel filename];
}
Run Code Online (Sandbox Code Playgroud)
六年后,我发现这个问题很有用,并且由于没有快速的答案,所以这里有一个快速的解决方案。
您会发现两个示例,一个作为独立窗口,另一个作为表格。
斯威夫特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
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10346 次 |
最近记录: |