PDA

View Full Version : NSOpenPanel - I'm doing something wrong




knightlie
May 18, 2012, 12:13 PM
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?



gnasher729
May 18, 2012, 12:42 PM
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?

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?

knightlie
May 18, 2012, 01:22 PM
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?

Yes, even if the block is empty I get the same behaviour.

knightlie
May 18, 2012, 03:27 PM
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.