Cocoa trouble with sheets

Discussion in 'Mac Programming' started by misee, Jan 4, 2011.

  1. misee, Jan 4, 2011
    Last edited: Jan 4, 2011

    misee macrumors member

    Joined:
    Jul 4, 2010
    #1
    Hi

    I'm having a little trouble with a sheet. I have a document based application, that should show a sheet right after opening a document (the sheet is attached to the document window). I have subclassed NSWindowController (as the documents window controller) and ran the sheet from the windowDidLoad. Now my problem is, that the sheet is not displayed correctly. It is shown as if it were a normal window, except that it does not have a title bar. This is the log I get:

    These are the methods that were called (I got these from setting the CGErrorBreakpoint):
    Code:
    #0	0x7fff847e847f in CGErrorBreakpoint
    #1	0x7fff84872a5a in CGSGlobalErrorv
    #2	0x7fff8468e5ad in _CGSFindSharedWindow
    #3	0x7fff8467cd0f in _CGSWindowByID
    #4	0x7fff846adc26 in CGSCopyWindowProperty
    #5	0x7fff846adad9 in CGSGetWindowProperty
    #6	0x7fff846adaa6 in windowGroup
    #7	0x7fff846ad5c8 in CGSOrderWindowListWithGroups
    #8	0x7fff846ad3f7 in CGSOrderWindowWithGroup
    #9	0x7fff8478a93c in CGSOrderPanelIfActive
    #10	0x7fff83952f65 in -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:]
    #11	0x7fff83b94103 in -[NSApplication _orderFrontModalWindow:relativeToWindow:]
    #12	0x7fff83b93b6b in -[NSApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:]
    #13	0x1000078f7 in -[DocumentGroup _runSheetForOpeningUnopenedFiles] at DocumentGroup.m:304
    
    I "solved" the problem by waiting 0.01 seconds before opening the sheet. I used NSTimer to do this.

    I'm running Mac OS X 10.6.5.

    I got the same error when I ran a sheet from awakeFromNib in a clean project which I created only to find out if this bug is due to my project settings or something similar.

    So my question: Am I doing something wrong? Are there any better workarounds than using a timer?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    As you've found out it you can't successfully run a modal sheet (or dialog) from within the windowDidLoad because the window loading sequence is not yet complete.

    Instead of using a timer, try this code inside windowDidLoad.

    Code:
    [self performSelectorOnMainThread:@selector(_runSheetForOpeningUnopenedFiles)
                           withObject:nil 
                        waitUntilDone:NO];
    
    This will schedule _runSheetForOpeningUnopenedFiles to be called outside of the window loading sequence.
     
  3. misee thread starter macrumors member

    Joined:
    Jul 4, 2010
    #3
    Wow, thanks! I guess I didn't think far enough (or maybe too far?). I'm really glad I can dump that ugly NSTimer solution.
     

Share This Page