Re: Loading PersistentDocuments into the same window
Re: Loading PersistentDocuments into the same window
- Subject: Re: Loading PersistentDocuments into the same window
- From: Quincey Morris <email@hidden>
- Date: Fri, 30 Apr 2010 10:17:35 -0700
On Apr 30, 2010, at 08:56, Brad Stone wrote:
> I want to open NSPersistentDocuments and load them into the same window one at a time.
Excuse the soapbox, but you do realize that from the outside looking in, this *seems* like a terrible idea? If your documents really are documents in the user interface sense, then what's the reason for inventing a new document-handling metaphor for users? If they're not really documents, then why try to shoehorn them into the NSDocument metaphor?
Keep in mind that Core Data persistent stores don't really fit the standard document metaphor very well, so that NSPersistentDocument is already a little bit broken. (Ask the people who've had trouble with Save As, for example.) Piling on another layer of metaphorical abuse seems like a prescription for great pain. But anyway ...
> - (IBAction)newBookTwo:(id)sender {
> NSDocumentController *dc = [NSDocumentController sharedDocumentController];
> NSURL *url = [NSURL fileURLWithPath:[@"~/Desktop/File 2.binary" stringByExpandingTildeInPath]];
>
> NSError *error;
> MainWindowDocument *thisDoc = [dc openDocumentWithContentsOfURL:url display:NO error:&error];
>
> [self setDocument:thisDoc];
> [self setManagedObjectContext:[thisDoc managedObjectContext]];
> }
Here's what I'd try (apologies if you've tried this already; all typed in mail):
1. Create a global variable:
MyWindowController* recycledWindowController = nil;
2. In the window controller:
- (IBAction)newBookTwo:(id)sender {
NSDocumentController *dc = [NSDocumentController sharedDocumentController];
NSURL *url = [NSURL fileURLWithPath:[@"~/Desktop/File 2.binary" stringByExpandingTildeInPath]];
recycledWindowController = self;
[self.document removeWindowController: self];
NSError *error;
MainWindowDocument *thisDoc = [dc openDocumentWithContentsOfURL:url display:NO error:&error];
}
3. In the document:
- (void) makeWindowControllers {
if (! recycledWindowController) {
recycledWindowController = ... // create a new window controller
recycledWindowController.shouldCloseDocument = YES;
}
[self addWindowController: recycledWindowController];
[recycledWindowController noteChangedDocument]; // or send a notification, or have the window controller KVO-observe its own 'document' property
recycledWindowController = nil;
}
Something like that.
_______________________________________________
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