【问题标题】:NSOpenPanel SheetNSOpenPanel 工作表
【发布时间】:2012-02-09 21:00:45
【问题描述】:

这就是我将打开的面板显示为浮动窗口的方式。

有人可以帮助我将面板作为工作表运行吗?窗口对象是 mWindow。我使用的许多标准代码都已贬值。

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"mp3", @"mp2", @"m4a", nil];

[openPanel setAllowsMultipleSelection: NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanCreateDirectories:NO];
[openPanel setCanChooseFiles:YES];
[openPanel setAllowedFileTypes:fileTypes];

NSString * filePath = @"~/Desktop";
filePath = [filePath stringByExpandingTildeInPath];
NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 
[openPanel setDirectoryURL:fileURL];

NSInteger clicked = [openPanel runModal];
if (clicked == NSFileHandlingPanelOKButton) {
    for (NSURL *url in [openPanel URLs]) {
        NSString *urlString = [url path];
        [input setStringValue:urlString];
        NSString *myString = [input stringValue];
        NSString *oldPath = [myString lastPathComponent];
        [inputDisplay setStringValue:oldPath];
    }
}

【问题讨论】:

    标签: macos cocoa nsopenpanel


    【解决方案1】:

    很简单,它就在文档中,尽管您可能错过了它,因为相关方法实际上是 NSSavePanel 的一部分,NSOpenPanel 继承自该方法。

    假设您的目标是 Sn​​ow Leopard 或更好,因此可以访问块,只需将您的 runModal 调用替换为以下内容:

    [openPanel beginSheetModalForWindow:mWindow completionHandler:^(NSInteger result) {
    
        if (result == NSFileHandlingPanelOKButton) {
    
            // Do something.
        }
    }];
    

    【讨论】:

    • 对不起。我只是省略了另一个 } after ];现在工作正常。感谢您的帮助。
    • mWindow ?什么是 mWindow?
    • @AviramNetanel,这是您希望保存面板成为工作表的窗口。
    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2011-12-28
    相关资源
    最近更新 更多