Search code examples
objective-ctitaniumdesktopappceleratornsalert

Block until NSAlert (shown as a modal sheet) is dismissed


I'm currently learning (by doing) objective-c by implementing a feature I felt is missing in the Titanium Appcelerator Desktop SDK: A way to do modal dialog with custom button texts and optionally to display them as a "sheet".

All is dandy and working, however, when displaying the NSAlert as a "sheet" my method that's creating the alert returns immediately and that's what I want to prevent.

The method's creating the alert returns an int (the return code from the NSAlert).

The code inside basically boils down to:

int returnCode = -1;
if (displayAsSheet) {
    [alert beginSheetModalForWindow:nativeWindow modalDelegate:delegate didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];

} else {
    returnCode = [alert runModal];
}

return returnCode;

The modalDelegate is an object that's implementing the needed:

- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;

and for now it just does a NSLog of the returnCode.

So my question is:

How can I block my method from returning until the "sheet" has been dismissed?

Or am I going about this the wrong way?


Solution

  • You must start a modal session for you sheet after showing it and stop the session after closing sheet.

    Check this: https://github.com/incbee/NSAlert-SynchronousSheet, I think it will be helpfull.