代码如下:

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

 

 posted on 2015-04-08 12:52  子叶_Sky  阅读(3821)  评论(0编辑  收藏  举报