【发布时间】:2018-03-06 08:17:32
【问题描述】:
我尝试在我的程序中使用NSOpenPanel,但它根本不起作用,因为 NSOpenpanel 不在主线程上运行。
这是我的代码
NSString *strURL;
NSOpenPanel *fileContents;
NSURL *panelURL;
NSArray *fileTypes = [NSArray arrayWithObjects:@"strings", @"STRINGS", nil];
fileContents = [NSOpenPanel openPanel];
[fileContents setCanChooseDirectories:NO];
[fileContents setCanChooseFiles:YES];
[fileContents setAllowedFileTypes:fileTypes];
[fileContents setAllowsMultipleSelection:NO];
NSInteger openPanelButton = [fileContents runModal];
if(openPanelButton == NSModalResponseOK)
{
panelURL = [fileContents URL];
strURL = panelURL.absoluteString;
}
NSArray *linesReadOnly = [strURL componentsSeparatedByString:@"\n"];
我尝试了以下两个代码:
[fileContents performSelectorOnMainThread:@selector(runModal) withObject:nil waitUntilDone:YES];
和
dispatch_sync(dispatch_get_main_queue(), ^{
//do UI stuff
});
在主线程上获取它根本不起作用。我做错了什么?
【问题讨论】:
-
就 Objective-C 而言,这里已经彻底讨论了 NSOpenPanel 的主题。没有什么新鲜事。
标签: objective-c xcode multithreading macos xcode9