How to enforce a single NSDocument ... or should I do something else?
How to enforce a single NSDocument ... or should I do something else?
- Subject: How to enforce a single NSDocument ... or should I do something else?
- From: Martin Stanley <email@hidden>
- Date: Tue, 5 Jan 2010 11:45:35 -0500
I have a Core-Date application that uses the Cocoa Document architecture (NSPersistentDocument) and would like to ensure that the user only can have 1 document open at a time. Think of this application as similar to Mail.app or Addressbook.app, etc. except that I would like the user to be able to open different persistent stores at will, just not simultaneously.
(As an aside, the reason for this is because my document has many related windows and I think it would be confusing for the user. It would not be obvious which auxiliary window relates to which document. I may fix this in the future by using the concept of Inspectors, but at this point I'm not sure if this is the correct model.)
I searched extensively and came up with the recommendation that I subclass NSDocumentController and override:
- (id)openDocumentWithContentsOfURL:display:error:
This was recommended over simply trapping the open menu item(s).
This seemed very promising until I ran into a stumbling block. In openDocumentWithContentsOfURL:display:error: I check to see if there is an already open document and if so call:
canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:
with an appropriate delegate and selector. The problem is that this returns immediately, even in the case when the document is dirty and it presents a modal dialog to the user. Because of this, the 2nd document is opened before I have a chance to deal with the first one.
All of this is making me wonder if I'm taking the wrong approach.
Should I figure out a way to prevent the 2nd document from opening before the first one is either saved or abandoned: (override canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: in my NSPersistentDocument subclass)? This feels like the solution is getting uglier by the minute.
Or is there a much easier way to accomplish what I want to do? Or should I be looking at a entirely different approach for my application?
Thanks,
Martin
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden