NSOpenPanel - Everything deprecated?

前端 未结 3 1766
花落未央
花落未央 2021-02-07 23:43

I\'ve been trying to get a window to show up asking the person to choose a file, and I eventually did. The problem is, Xcode complains that the method I\'m using is deprecated.

3条回答
  •  無奈伤痛
    2021-02-08 00:19

    In 10.6, there was a few changes to this classes. One of the benefits is that there is now a block-based API.

    Here is a code snippet on how to use that:

    NSOpenPanel *panel = [[NSOpenPanel openPanel] retain];
    
    // Configure your panel the way you want it
    [panel setCanChooseFiles:YES];
    [panel setCanChooseDirectories:NO];
    [panel setAllowsMultipleSelection:YES];
    [panel setAllowedFileTypes:[NSArray arrayWithObject:@"txt"]];
    
    [panel beginWithCompletionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton) {
    
            for (NSURL *fileURL in [panel URLs]) {
                // Do what you want with fileURL
                // ...
            }
        }
    
        [panel release];
    }];
    

提交回复
热议问题