Re: How to enforce a single NSDocument ... or should I do something else?
Re: How to enforce a single NSDocument ... or should I do something else?
- Subject: Re: How to enforce a single NSDocument ... or should I do something else?
- From: Martin Stanley <email@hidden>
- Date: Wed, 6 Jan 2010 08:10:24 -0500
Thanks very much, Bill. I like your approach and in fact I think our scenarios are quite similar so I'm going to implement your solution. By the way, I can't seem to find the "document-already-open" error. Is it a standard error code?
Martin
On 2010-01-05, at 3:26 PM, Bill Cheeseman wrote:
>
> On Jan 5, 2010, at 11:45 AM, Martin Stanley wrote:
>
>> 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.
>
> I do this by overriding -openDocumentWithContentsOfURL:display:error:. In my override:
>
> 1. If this document is already open, I call -makeKeyAndOrderFront:.
>
> 2. If another document of the same class is already open, I set outError to a document-already-open error and present an alert giving the user some options.
>
> 3. If no document is open, I call super to open a new one.
>
> In other words, I take the reverse of your approach. I don't automatically close the open document, but instead report an error and let the user decide whether to close the already-open document. Perhaps your scenario makes that unsuitable, but it works perfectly for my scenario, where the notion is that there is one and only one "current" document of this class and it should be replaced only rarely.
>
>
> --
>
> Bill Cheeseman
> email@hidden
>
_______________________________________________
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