Closing documents on app termination issues

Discussion in 'Mac Programming' started by titaniumdecoy, Jan 5, 2008.

  1. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #1
    The following is my implementation of the reviewUnsavedDocumentsWithAlertTitle:cancellable:delegate:didReviewAllSelector:contextInfo: method of NSDocumentController:

    Code:
    - (void)reviewUnsavedDocumentsWithAlertTitle:(NSString *)title cancellable:(BOOL)cancellable delegate:(id)delegate didReviewAllSelector:(SEL)didReviewAllSelector contextInfo:(void *)contextInfo
    {
        // close each window with unsaved document(s) here
        
        // this is called immediately before each window can close & save its documents
        [super reviewUnsavedDocumentsWithAlertTitle:title cancellable:cancellable delegate:delegate didReviewAllSelector:didReviewAllSelector contextInfo:contextInfo];
    }
    I want to close and save each document manually, preferably without ever calling the super. However, if I neglect to call the super the application never terminates and I suspect other housekeeping is not take care of.

    When each unsaved window containing unsaved document(s) is closed, I put up an alert listing those documents by calling NSAlert's beginSheetModalForWindow:modalDelegate: method. This lets the method return while the sheet is still open.

    I need some way to wait until the alert for each window has completed (and calls alertDidEnd:returnCode:contextInfo:) so that I can call the super from the reviewUnsavedDocumentsWithAlertTitle:... method. I would use callback like NSAlert but then I am no longer able to call the aforementioned super.

    Thanks for any input.
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    Personally editing these kind of methods seems to do more harm than good, and they always call stuff they don't mention. Do you really need to do this your own way? Remember you'll lose consistency with other Mac applications.


    By the way, I wouldn't say you have a good forum thread name, prehaps "closing document issues" or something would be better.
     
  3. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #3
    @Eraserhead: Thanks for the reply. Unfortunately, I have multiple documents in a single window via a tabbed interface, and I want to prompt the user to save with a single dialog rather than one for each document in the window: so it would appear that I really do need to customize this method.

    As for the method calling stuff it doesn't mention: that is why I am hoping to find a way to call the super rather than rewriting it completely. However, that is an option. Is it possible to find the source code for Apple's implementation of NSDocumentController?

    I also made the thread title more appropriate as per your suggestion.
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    a) Can't you set the delegate of the alert to your document controller?

    b) Can't you call a selector of the form
    Code:
    - (void)documentController:(NSDocumentController *)docController  didReviewAll: (BOOL)didReviewAll contextInfo:(void *)contextInfo
    
    from your method or doesn't that work?
     
  5. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #5
    Thanks for the suggestions. I ended up taking your original suggestion and found another way to go about it. I intercept the terminate: message in an NSWindowController subclass and perform the necessary actions at that point. (In case you are wondering, NSApplicationWillTerminateNotification is posted after all unsaved documents are closed, which is far too late for my needs.)
     

Share This Page