0
votes

I'm in the midst of debugging an extremely unusual problem, and I was wondering if anybody might have any insight into what might be going wrong:

In a controller class from a NIB, I take an NSPanel from that same NIB, and then show it app modally on a NSWindow (that was created by hand in code):

[[NSApplication sharedApplication] beginSheet: myPanel
                               modalForWindow: window
                                modalDelegate: self
                               didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
                                  contextInfo: nil];
[[NSApplication sharedApplication] runModalForWindow: myPanel];

Now, when the "finish" button on that sheet is clicked, I run some code to disable some buttons and fire off a thread to make sure the user input is valid (I have to validate with a remote service). This thread is fired from a separate validator object I create:

// controller calls:
[validator validateCreds: creds
           notify: @selector(validationComplete:)
           onObject: self];

// validator object
validateInfo: (NSDictionary *)parms
      notify: (SEL)notifySelector
    onObject: (id)notifyObject
{
 // build up data with parms and notify info
 [[NSThread detachNewThreadSelector: @selector(remotevalidate:)
            toTarget: self withObject: data];
}

Next, when the validation is finished, the validator notifies my controller object:

[notifyObject performSelectorOnMainThread: notifySelector
              withObject: results waitUntilDone: NO];

And then my controller object, in the method that the validator object calls, kills the dialog:

- (void)validationComplete: (id)data
{
   [[NSApplication sharedApplication] stopModal];
   [createTwitterPanel orderOut: nil];
   [[NSApplication sharedApplication] endSheet: createTwitterPanel
                                      returnCode: NSOKButton];
}

- (void)sheetDidEnd:(NSWindow *)sheet
         returnCode:(int)returnCode
        contextInfo:(void  *)contextInfo
{
    m_returnCode = returnCode;
}

My problem: Although the panel is closed / disappears, the top NSApp runModalForWindow: does not exit until some system event is sent to the window that was showing the dialog. Trying to move, resize, or do anything to the window, or otherwise switching away from the application suddenly causes the method to exit and execution to continue. No amount of waiting seems to help, otherwise, however.

I have verified that all methods being invoked on the controller class are all being invoked on the main app thread.

An even more interesting clue is that the dialog has two controls, a WebView, and an NSTextField: Even if I force the exit of runModalForWindow: by clicking on the window, TABbing between the two controls remains screwed up — it simply never works again. It's like my event loop is horked.

I've tried changing validationComplete: to instead post a notification to the main thread, and I've also played with the waitUntilDone on the performSelectorOnMainThread method, all to no effect.

Any ideas? Things I should try looking at?

1
This is a 10.5 targeted app, running Xcode 3.2.1 on 10.6.1MarcWan

1 Answers

3
votes

From the NSApplication documentation:

abortModal must be used instead of stopModal or stopModalWithCode: when you need to stop a modal event loop from anywhere other than a callout from that event loop. In other words, if you want to stop the loop in response to a user’s actions within the modal window, use stopModal; otherwise, use abortModal. For example, use abortModal when running in a different thread from the Application Kit’s main thread or when responding to an NSTimer that you have added to the NSModalPanelRunLoopMode mode of the default NSRunLoop.

So, I learned something today.