NSOpenPanel-Object C組件-打開對話框-選擇文件/文件夾得到路徑

 

1. NSOpenPanel的beginWithCompletionHandler:^(NSInteger result),打開文件對話框時,對話框出如今屏幕的中央。url

    
    NSMutableArray * fileURLArray = [[NSMutableArray alloc] init];
    
    NSOpenPanel * mySelectPanel = [NSOpenPanel openPanel];
    [mySelectPanel setCanChooseDirectories:YES];
    [mySelectPanel setCanChooseFiles:YES];
    [mySelectPanel setCanCreateDirectories:YES];
    [mySelectPanel setAllowsMultipleSelection:YES];
    [mySelectPanel setResolvesAliases:YES];
    
    //界面出如今電腦屏幕中央
    [mySelectPanel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSModalResponseOK) {
            NSLog(@"OK");
            for (NSURL * url in [mySelectPanel URLs]){
                NSString * path = [NSString stringWithString:[url path]];
                //path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                //path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [fileURLArray addObject:path];
                NSLog(@"%@", path);
            }
            for (NSString * a in fileURLArray) {
                NSLog(@"%@", a);
            }
            
            NSLog(@"%@", fileURLArray);
            
        } else if (result == NSModalResponseCancel) {
            NSLog(@"Cancel");
        } else if (result == NSModalResponseStop) {
            NSLog(@"Stop");
        }
    }];

 

2. NSOpenPanel的beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result),打開文件對話框時,對話框依附在操做界面下。spa

    
    NSMutableArray * fileURLArray = [[NSMutableArray alloc] init];
    
    NSOpenPanel * mySelectPanel = [NSOpenPanel openPanel];
    [mySelectPanel setCanChooseDirectories:YES];
    [mySelectPanel setCanChooseFiles:YES];
    [mySelectPanel setCanCreateDirectories:YES];
    [mySelectPanel setAllowsMultipleSelection:YES];
    [mySelectPanel setResolvesAliases:YES];
    
    //對話框依附在操做界面下
    [mySelectPanel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
        if (result == NSModalResponseOK) {
            NSLog(@"OK");
            for (NSURL * url in [mySelectPanel URLs]){
                NSString * path = [NSString stringWithString:[url path]];
                //path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                //path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [fileURLArray addObject:path];
                NSLog(@"%@", path);
            }
            for (NSString * a in fileURLArray) {
                NSLog(@"%@", a);
            }
            
            NSLog(@"%@", fileURLArray);
            
        } else if (result == NSModalResponseCancel) {
            NSLog(@"Cancel");
        } else if (result == NSModalResponseStop) {
            NSLog(@"Stop");
        }
    }];

相關文章
相關標籤/搜索