ziyeSky

代码如下:

   NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    [openPanel setPrompt: @"打开"];
    
    openPanel.allowedFileTypes = [NSArray arrayWithObjects: @"txt", @"doc", nil];
    openPanel.directoryURL = nil;
    
    [openPanel beginSheetModalForWindow:[self gainMainViewController] completionHandler:^(NSModalResponse returnCode) {
        
        if (returnCode == 1) {
            NSURL *fileUrl = [[openPanel URLs] objectAtIndex:0];
            // 获取文件内容
            NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:fileUrl error:nil];
            NSString *fileContext = [[NSString alloc] initWithData:fileHandle.readDataToEndOfFile encoding:NSUTF8StringEncoding];
            
            // 将 获取的数据传递给 ViewController 的 TextView
            ViewController *mainViewController = (ViewController *)[self gainMainViewController].contentViewController;
            mainViewController.showCodeTextView.string = fileContext;
        }
    }];

 

分类:

技术点:

相关文章:

  • 2021-10-12
  • 2021-04-01
  • 2021-08-22
  • 2021-12-08
猜你喜欢
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案