NSOpenPanel - I'm doing something wrong

Discussion in 'Mac Programming' started by knightlie, May 18, 2012.

  1. knightlie macrumors 6502a

    Feb 18, 2008
    Something weird is happening with an NSOpenPanel. Here's the code:

    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    [openPanel setAllowsMultipleSelection:NO];
    [openPanel setAllowedFileTypes:[NSArray arrayWithObject:@"categories"]];
    [openPanel beginSheetModalForWindow:[self.document windowForSheet] completionHandler:^(NSInteger result){
        if (result==NSFileHandlingPanelOKButton) {
            // Process the file		
    (This is inside a simple class which has access to an NSPersistentDocument via the document property).

    When this runs the open panel appears, the selected file is processed and everything seems normal. "Processing" a file in this instance adds objects to the managed object context. But... once this code has run the Quit menu option and various others are disabled - as if the panel is still in place, and I have to force quit. I'm using an NSSavePanel in the same way without the same issue.

    I haven't used Blocks very much in this manner - am I doing something wrong?
  2. gnasher729 macrumors P6


    Nov 25, 2005
    Quite possible that the code inside the block causes the problem. Things that we can see are always good for causing trouble. Try it with the code as it is. Obviously any files won't get processed in a meaningful way, but do you still get the bad behaviour?
  3. knightlie thread starter macrumors 6502a

    Feb 18, 2008
    Yes, even if the block is empty I get the same behaviour.
  4. knightlie thread starter macrumors 6502a

    Feb 18, 2008
    I think I found it. The code I posted takes place in a method whose return value indicates success. When the OpenPanel appeared the method exited immediately and returned NO, which resulted in the calling code trying to show an error sheet. It ended up with the document window displaying a sheet which wasn't visible.

    I need to get used to how Blocks work - I assumed the beginSheetForModal method would net return until the block had been called, which doesn't appear to be the case.

Share This Page