1
我需要打開一個文本文件並處理其中的每一行。我無法關閉NSOpenPanel,因此我可以繼續處理。下面結合了我在幾年前在這裏找到的代碼,我希望有人發現了一種不同的方式。 NSSavePanel按預期執行,單擊按鈕時關閉。如何關閉NSOpenPanel
- (IBAction)loadSettings:(id)sender
{
NSString *t = [self splitSettings:@"k"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC/100), dispatch_get_main_queue(), ^(void){
// some other method calls here
});
int i = 4; // so I have a breakpoint
}
- (NSString*)splitSettings:(NSString*)inFile
{
NSOpenPanel *zOpenPanel = [NSOpenPanel openPanel];
NSArray *arrayOfExtensions = [NSArray arrayWithObject:@"txt"];
[zOpenPanel setAllowedFileTypes:arrayOfExtensions];
NSInteger intResult = [zOpenPanel runModal];
if (intResult == NSFileHandlingPanelCancelButton) {
NSLog(@"readUsingOpenPanel cancelled");
return @"Cancelled";
}
NSURL *zUrl = [zOpenPanel URL];
// read the file
NSString * zStr = [NSString stringWithContentsOfURL:zUrl encoding:NSASCIIStringEncoding error:NULL];
return zStr;
}
我讀過你的參考資料,下載了你的項目並追蹤了它。我無法弄清楚如何將它應用到我的案例中。 – Mike