Closing NSDocument

Discussion in 'Mac Programming' started by titaniumdecoy, Dec 16, 2007.

  1. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #1
    Hi all,

    I have a number of tabs in a window, each with a corresponding document. When I close a tab I want it to close its document such that it asks to save changes. The -close method of NSDocument "closes the document immediately, without asking users if they want to save the document." Unfortunately I can't figure out how to close a document without bypassing the option to save.

    Thanks for any help.

    EDIT: I tried this:

    Code:
    [document saveDocument:nil];
    [document close];
    The save dialog appears for an instant but disappears when close is called. How can I "wait" until the document has saved before closing it?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Have you tried closing all the windows (or the only window) containing a view of the document?
     
  3. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #3
    The bigger problem I am having is that none of the documents are ever marked as dirty; i.e., isDocumentEdited always returns NO. I find this strange since each document contains an NSTextView with undo enabled.

    Although I can undo and redo changes to each document, when I get a document's NSUndoManager and query its canUndo method, it always returns NO.

    What am I missing?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    AFAIK, NSTextView isn't automatically bound to the NSDocument undo manager. You need to update the undo manager when changes are made to the text view.
     
  5. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #5
    Ordinarily it seems to if you click "Allow Undo" in IB...
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    But that is using the text view's built-in undo manager. You need to figure out how to tie that in to the NSDocument undo manager.
     
  7. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    It looks good on paper but it is so annoying in reality IMO.

    You need to look at undoManagerForTextView: and work out how to merge that with NSDocument's. I really don't know how but I'm curious to find out.
     
  8. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #8
    Thanks for the tips. It was actually an astonishingly easy fix; I simply (set NSDocument as the NSTextView delegate and) implemented undoManagerForTextView to return the document's undoManager. :)

    I am still having problems closing documents, though. Should I allow each document to retain its original window controller since that window will never be seen? When I add the main (tabbed) window controller to the document's list of window controllers before it is closed it closes that without calling its windowShouldClose method.

    So basically, I am calling NSDocument-close which closes the document without asking to save. How can I close the document such that it asks to save? There has to be a simple way to do this that I am overlooking.

    I am back to one of the questions I started with: Calling [document saveDocument:nil];[document close]; flashes the save dialog on screen for a brief moment but quickly closes the document. What can I do about this? Or better yet, is there a way to get the document itself to ask to save when it is dirty and closing? Thanks.

    EDIT: To put it more concisely, how can I close a document in such a way that it calls canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: ?
     
  9. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #9
    I solved the problem. To close the document I sent a closeDocument message to the First Responder, which called the closeDocument method of NSDocumentController. I implemented that method to call the current document's canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: method. The rest takes care of itself. Thanks for the help!
     

Share This Page