使用Objective-C为OS X应用程序打开文档

使用Objective-C为OS X应用程序打开文档,objective-c,macos,file,cocoa,Objective C,Macos,File,Cocoa,我实现了一个简单的应用程序,它基本上输出关于给定三角形的信息(见图)。我已经通过编程创建了三角形。我想改进这个示例,提供一种机制来打开say.tri文件及其侧面(例如3 4 5)。我怎样才能做到这一点?我做了一些研究,发现有一种叫做openDocument的方法。。我将如何在我的应用程序中使用它?有人能给我举个例子说明如何做到这一点吗?显然,它不是一个基于文档的应用程序。。我在github上获得了以下代码: 我设法使面板显示如下 - (IBAction)openDocument:(id)send

我实现了一个简单的应用程序,它基本上输出关于给定三角形的信息(见图)。我已经通过编程创建了三角形。我想改进这个示例,提供一种机制来打开say.tri文件及其侧面(例如3 4 5)。我怎样才能做到这一点?我做了一些研究,发现有一种叫做openDocument的方法。。我将如何在我的应用程序中使用它?有人能给我举个例子说明如何做到这一点吗?显然,它不是一个基于文档的应用程序。。我在github上获得了以下代码:


我设法使面板显示如下

- (IBAction)openDocument:(id)sender{
    NSOpenPanel* openPanel = [NSOpenPanel openPanel];

    [openPanel setAllowedFileTypes:[NSArray arrayWithObjects:@"tri", @"qua",nil]];
    [openPanel runModal];

}

打开文件后,我无法更改NSTableView的值。代码如下:

- (IBAction)openDocument:(id)sender{
    NSOpenPanel* panel = [NSOpenPanel openPanel];
    [panel setAllowedFileTypes:[NSArray arrayWithObjects:@"tri", @"qua",nil]];

    [panel beginWithCompletionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton) {
            NSURL*  file = [[panel URLs] objectAtIndex:0];

            [self performSelectorInBackground:@selector(triangle:) withObject:file];

        }

    }];
}


-(void) triangle:(NSURL *)file{

        NSError *error;

        NSString *words = [[NSString alloc] initWithContentsOfURL:file encoding:NSUTF8StringEncoding error:&error];

        NSLog(@"%@", words);

        NSArray* lines = [words componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

        NSMutableArray *shapes = [[NSMutableArray alloc] init];

        while (lines) {

            NSArray*info = [words componentsSeparatedByString:@";"];

            // Creates triangles to be populated
            CGFloat side1 = (CGFloat)[info[0] floatValue];
            CGFloat side2 = (CGFloat)[info[1] floatValue];
            CGFloat side3 = (CGFloat)[info[2] floatValue];

            Triangle *triangle = [[Triangle alloc] initWithSides:side1 side:side2 andSide:side3];

            [shapes addObject:triangle];
        }

        self.formsArray = shapes;

        [self performSelectorOnMainThread:@selector(updateTableView) withObject:nil waitUntilDone:YES];

}

-(void)updateTableView{
    [self.tableView reloadData];
}

@end

我应该搜索什么?我应该实现自己的函数来打开文件还是使用openDocument函数?我尝试过在API上搜索,但我不太理解..当我声明此方法:-(IBAction)openDocument:(id)sender{}我得到了打开菜单,但什么也没有发生..我设法使面板显示如下:。。。也要考虑<代码> -NeXSeTimeDalFoFiels:FultAutoHuntLe: 如果打开的东西将在一个特定的现有窗口中结束,而不是被打开为新的东西。项目在我的GITHUB上。希望有人能帮助我。