Re: CORE DATA: open last used document ??
Re: CORE DATA: open last used document ??
- Subject: Re: CORE DATA: open last used document ??
- From: "dan pahlajani" <email@hidden>
- Date: Sun, 26 Mar 2006 09:53:25 -0800
Thanks Pierce. I tried the code and it works very well. In fact it is
better than what I originally had in mind.
As always, appriciate your help.
Thanks,
Dan
On 3/23/06, Pierce T. Wetter III <email@hidden> wrote:
>
> On Mar 22, 2006, at 5:09 PM, dan pahlajani wrote:
>
> > Hello everyone,
> >
> > How do I open the last used document while using Core Data --
> > NSPersistenDocument?
> >
> > Before the days of Core Data, I was able to do so in the app
> > delegate by
> > accessing the recent docs array. With NSPersistenDocument I don't
> > know which
> > message I should send to it once I have the file's URL.
>
> I'm doing it this way:
>
> in AppController.m:
>
> - (BOOL) applicationShouldOpenUntitledFile: (NSApplication *) sender
> {
> if ([[NSUserDefaults standardUserDefaults]
> objectForKey:@"lastFileURL"])
> return NO;
> return YES;
> }
>
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
> {
> if ([[[NSDocumentController sharedDocumentController] documents]
> count]==0)
> {
> NSData *fileURLData=[[NSUserDefaults standardUserDefaults]
> objectForKey:@"lastFileURL"];
> if (fileURLData)
> {
> NSError *error;
> NSURL *fileURL=[NSUnarchiver unarchiveObjectWithData:
> fileURLData];
> [[NSDocumentController sharedDocumentController]
> openDocumentWithContentsOfURL: fileURL
> display: YES
> error: &error];
>
> }
>
> in My document.m
>
> - (BOOL)writeToURL:(NSURL *)absoluteURL
> ofType:(NSString *)typeName
> forSaveOperation:(NSSaveOperationType)saveOperation
> originalContentsURL:(NSURL *)absoluteOriginalContentsURL
> error:(NSError **)error
> {
> [[[NSUserDefaultsController sharedUserDefaultsController] values]
> setValue: [NSArchiver archivedDataWithRootObject:
> absoluteOriginalContentsURL] forKey:@"lastFileURL"];
> return [super writeToURL: absoluteURL
> ofType: typeName
> forSaveOperation: saveOperation
> originalContentsURL: absoluteOriginalContentsURL
> error: error];
> }
>
>
> That is, when someone saves a file, I store the URL in the
> preferences file. In "shouldOpenUntitledFile" I look for the URL and
> return NO if there is one stored, and in
> applicationDidFinishLuanching I tell the document controller to open
> that URL.
>
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden