我需要打开一个文本文件并处理其中的每一行。我无法让NSOpenPanel关闭,因此我可以继续处理。下面是几年前我在这里找到的代码,我希望有人发现了一种不同的方法。NSSavePanel按预期执行,在单击按钮时关闭。
- (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;
}发布于 2015-05-20 01:43:36
Check out my example project here that I used to figure this stuff out, CocoaSheets。但请注意,这适用于使用任何模型表的一般情况,而不仅仅是NSOpenPanel。也许有人会张贴NSOpenPanel的具体案例,在任何情况下,这应该会有所帮助,我希望。
使用以下命令启动模式窗口
[[self window] beginSheet:self.sheetWindowController.window completionHandler:^(NSModalResponse returnCode) {
switch (returnCode) {
case NSModalResponseCancel:
NSLog(@"%@", @"NSModalResponseCancel");
break;
case NSModalResponseOK:
NSLog(@"%@", @"NSModalResponseOK");
break;
default:
break;
}}];然后将cancel和OK按钮连接到以下操作方法。请注意,您使用sheetParent来结束工作表。
- (IBAction)cancelButtonAction:(id)sender {
[[[self window] sheetParent] endSheet:self.window returnCode:NSModalResponseCancel];
}
- (IBAction)OKButtonAction:(id)sender {
[[[self window] sheetParent] endSheet:self.window returnCode:NSModalResponseOK];
}https://stackoverflow.com/questions/30331914
复制相似问题