2012-02-09 34 views
5

Đây là cách tôi hiển thị bảng điều khiển mở dưới dạng cửa sổ nổi.NSOpenPanel Sheet

Ai đó có thể giúp tôi với việc chạy bảng điều khiển dưới dạng trang tính không? Đối tượng cửa sổ là mWindow. Phần lớn mã tiêu chuẩn tôi đang sử dụng được khấu hao.

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]; 
    } 
} 

Trả lời

12

Khá đơn giản, nó phải có trong tài liệu dù bạn có thể đã bỏ lỡ nó vì phương pháp có liên quan thực sự là một phần của NSSavePanel, từ đó NSOpenPanel thừa hưởng.

Giả sử bạn đang nhắm mục tiêu Snow Leopard hoặc tốt hơn và do đó có quyền truy cập vào các khối, nó chỉ là vấn đề thay thế cuộc gọi runModal của bạn với điều này:

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

    if (result == NSFileHandlingPanelOKButton) { 

     // Do something. 
    } 
}]; 
+0

cảm ơn - vẫn không thể có được nó. Tôi đang nhận lỗi phân tích cú pháp. – user1198008

+0

Vui lòng giải thích những lỗi bạn đang gặp phải. –

+0

Xin lỗi. Tôi chỉ đơn giản là bỏ qua một cái khác} * sau *]; Làm việc tốt ngay bây giờ. cảm ơn đã giúp đỡ. – user1198008

Các vấn đề liên quan