Hey Guys!
I'm stuck!
I am trying to create a custom modal dialog. I would like it to perform similarly to the NSSavePanel using a block as a completion handler.
I've copied only the important snippets I think are needed.
@implementation ModalWindowController
- (void)makeKeyAndOrderFront:(id)sender
modalToWindow:(NSWindow*)window
sourceRect:(NSRect)rect
completionHandler:(void (^)(NSInteger result))handler {
_handler = [handler retain];
session = [NSApp beginModalSessionForWindow:[self window]];
[[NSApplication sharedApplication] runModalSession:session];
[[self window] makeKeyAndOrderFrontCentered:self expandingFromFrame:rect];
}
- (IBAction)okButtonPressed:(id)sender {
[[self window] orderOut:self];
_handler(NSOKButton);
[NSApp endModalSession:session];
}
@end
Now I can call this using the code:
[self.modalWindowController makeKeyAndOrderFront:self
modalToWindow:[[self view] window]
sourceRect:sr
completionHandler:^(NSInteger result) {
NSLog(@"Inside Block");
if ( result == NSOKButton ) {
// do something interesting here
}
}];
NSLog(@"Errg");
All goes well however, after the method makeKeyAndOrderFront:modalToWindow:sourceRect:completionHandler: has completed it does not block the thread, so "Errg" will be printed even though the user has not selected "ok" or "cancel". The modal window is displayed at this point, where the user clicks OK and the _handler block is then executed. However if I am trying to access local variables in the block, and the app crashes as everything has cleaned up already.
What is the best approach to blocking the main thread from the makeKeyAndOrderFront:... method? Is this the right approach to implementing a completion handler using blocks?