Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

titaniumdecoy

macrumors member
Original poster
Oct 13, 2005
86
0
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?
 

titaniumdecoy

macrumors member
Original poster
Oct 13, 2005
86
0
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?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

titaniumdecoy

macrumors member
Original poster
Oct 13, 2005
86
0
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.
Ordinarily it seems to if you click "Allow Undo" in IB...
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.

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.
 

titaniumdecoy

macrumors member
Original poster
Oct 13, 2005
86
0
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: ?
 

titaniumdecoy

macrumors member
Original poster
Oct 13, 2005
86
0
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.