2012-02-09 55 views
5

这就是我如何将一个打开的面板显示为一个浮动窗口。NSOpenPanel Sheet

有人可以帮我把面板作为工作表运行吗?窗口对象是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]; 
    } 
} 

回答

12

很简单,这是正确的,在该文档虽然你可能已经错过了它,因为相关的方法实际上是NSSavePanel,从中NSOpenPanel继承的一部分。

。假定你的目标雪豹或更好,这样就可以使用块,它只是一个问题与此更换您的通话runModal:

[openPanel beginSheetModalForWindow:mWindow completionHandler:^(NSInteger result) { 

    if (result == NSFileHandlingPanelOKButton) { 

     // Do something. 
    } 
}]; 
+0

感谢 - 仍不能得到它。我收到解析错误。 – user1198008 2012-02-09 22:05:31

+0

请解释您收到的错误。 – 2012-02-09 22:48:21

+0

对不起。我简单地在*之后忽略了另一个} *;现在工作正常。谢谢您的帮助。 – user1198008 2012-02-09 23:03:26