NSOpenPanel stays open during debugging

274 views Asked by At

Is there a way to force the NSOpenPanel to close so I can see the screen while debugging? I can't see code behind it in Xcode while debugging and I can't move the panel either.

This is what I have:

- (IBAction)openImage:(id)sender {
    NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"jpg", @"JPG", nil];

    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setCanChooseDirectories:NO];
    [panel setCanChooseFiles:YES];
    [panel setAllowsMultipleSelection:NO];
    [panel setAllowedFileTypes:fileTypes];

    [panel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {

            self.image = [[NSImage alloc] initWithContentsOfURL:panel.URL];
            [panel close];


            [self doSomethingWithImage];

        }else{
        }
    }];
}

- (void) doSomethingWithImage {
    // if I put a breakpoint here, 
    // the NSOpenPanel is still on the screen and I can't move it.

}
2

There are 2 answers

1
Joshua Nozzi On

A simple workaround is to schedule -doSomethingWithImage on the main queue so the completion handler finishes (and the dialog closes) before -doSomethingWithImage is executed.

[panel beginWithCompletionHandler:^(NSInteger result) {
    if (result == NSFileHandlingPanelOKButton) {

        self.image = [[NSImage alloc] initWithContentsOfURL:panel.URL];
        [panel close];

      dispatch_async(dispatch_get_main_queue(), { () -> Void in

        [self doSomethingWithImage];

      });

    }else{
    }
}];
1
Money On

Personally I found two approaches:

  1. if you start your app, either move your app or Xcode to another desktop. So, when you run your app, it will jump to another desktop (where Xcode is located) when you reach a break point and the open panel does not get in the way with Xcode.

  2. Use beginSheetModalForWindow: The coding is almost the same, but the panel does not hide parts of Xcode while you are debugging. OpenPanel is run as a choose panel related to a window. For beginSheetModalForWindow you must indicate the related window (usually the main window), which is the only additional coding needed.