Home > database >  Problems with Replacing NSOpenPanel runModalForDirectory: with beginWithCompletionHandler:
Problems with Replacing NSOpenPanel runModalForDirectory: with beginWithCompletionHandler:

Time:09-09

I am having problems replacing the deprecated NSOpenPanel runModalForDirectory: with the newer beginWithCompletionHandler:.

The results are not as expected. It looks like beginWithCompletionHandler: does NOT wait for the user to finish with his or her selection!

Please let me know what I am doing wrong or suggest a different solution for replacing the deprecated method.

if ([destpath isEqualToString:@""])
    {
        long result;  
        NSOpenPanel *oPanel = [NSOpenPanel openPanel];
        [oPanel setAllowsMultipleSelection:NO];
        [oPanel setCanChooseDirectories:YES];
        [oPanel setCanChooseFiles:NO];
        
        result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil];
        if (result == NSModalResponseOK) 
        {
            destpath = [[oPanel URL] retain].path;
            NSLog(@"destpath is : %@", destpath);
        }
        else
            return;
    }
    
    NSLog(@"MADE IT HERE!! -- destpath is : %@", destpath);
            

I replaced the code with:

if ([destpath isEqualToString:@""])
    {
        // long result;  // FIXED!!  Changed int to long
        NSOpenPanel *oPanel = [NSOpenPanel openPanel];
        [oPanel setAllowsMultipleSelection:NO];
        [oPanel setCanChooseDirectories:YES];
        [oPanel setCanChooseFiles:NO];
                
        [oPanel beginWithCompletionHandler:^(NSInteger result) {
            if(result == NSModalResponseOK) {
                destpath = [[oPanel URL] retain].path;
                NSLog(@"destpath is : %@", destpath);
                }
            else
                return;
        }];
        
    }
    
    NSLog(@"MADE IT HERE!! -- destpath is : %@", destpath);
        

When I run the original code, I see the following NSLog messages:

022-09-04 12:20:18.175924-0400 iRipCD[69097:3332748] destpath is : /Users/grinch/Downloads
2022-09-04 12:20:18.175982-0400 iRipCD[69097:3332748] MADE IT HERE!! -- destpath is : /Users/grinch/Downloads

But the new code does not work. The program does NOT wait for the user to finish with his or her selection of the desired directory. I see the following NSLog messages:

2022-09-04 12:15:30.595589-0400 iRipCD[68991:3329339] MADE IT HERE!! -- destpath is :

2022-09-04 12:15:34.320552-0400 iRipCD[68991:3329339] destpath is : /Users/grinch/Downloads

FYI -- destpath is initilaized earler in the same file:

- (id)init
{
    self = [super init];

    task = nil;
    timer = nil;
    destpath = @"";
    taskOutput = [[NSMutableString alloc] init];
    fileList = [[NSMutableArray alloc] initWithCapacity:15];

    [(id) [NSApp delegate] setMaster:self];  // FIXED THIS!!  Added (id)

    return self;
}

CodePudding user response:

It looks like one MUST use runModal to ensure that the NSOpenPanel object waits for the user to choose the directory.

The beginWithCompletionHandler: method does NOT wait for the user to finish his or her selection.

The following code appears to work:

if ([destpath isEqualToString:@""])
{
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];

    [oPanel setAllowsMultipleSelection:NO];
    [oPanel setCanChooseDirectories:YES];
    [oPanel setCanChooseFiles:NO];

    // 
    // N.B. -- beginWithCompletionHandler: does not wait for the
    // user to finish with his or her selection!
    //               
    //[oPanel beginWithCompletionHandler:^(NSInteger result) {
    //     if(result == NSModalResponseOK) {
    //
    // Use runModal to ensure that the program waits for the user
    // to finish with his or her selection!
    //

    if ([oPanel runModal] == NSModalResponseOK)
    {
        destpath = [[[oPanel URL] path] retain]  // Also fixed memory bug!
        NSLog(@"destpath is : %@", destpath);
    }
    else
        return;         
 }
  • Related