Fwd: Need sample code for NSDocument / NSDocumentController

  • There is no sample code for the new -[NSDocumentController openDocumentWithContentsOfURL:display:completionHandler:]
    Why is there no sample project for a standard NSDocumentController/NSDocument based app?

    Can someone provide sample code for how to migrate from the deprecated
    - (id)openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)outError
    to the new approved
    -[NSDocumentController openDocumentWithContentsOfURL:display:completionHandler:]

    In particular, the hopefully soon-to-be-deprecated ^block syntax should always be documented
    with sample code of how it should be used.
    I need detailed documentation of the effect passing YES vs. NO for documentWasAlreadyOpen

    In our case, there can only be one document open at a time.
    However, NSDocument is useful for save, revert, edit, NSUndoManager, etc.

    The Cocoa docs state that NSDocumentController should rarely need to be subclassed.
    However, in Lion, I'm at a loss for why its complaining to me:

    -[NSDocumentController openDocumentWithContentsOfURL:display:completionHandler:] failed during state restoration. Here's the error:
    "The document “test.mboxshow” could not be opened. MyApp cannot open files in the “"MyApp.app Document” format.

    My attempts to override the new function instead does bad things… like no new windows can be drawn.
    My app seems to randomly do bad things, similar to what happens when there is an uncaught exception,
    although I see no exceptions being thrown.
    Cocoa spits out no log messages, and I have exhausted every conceivable debug method (NSException catch, NSZombie, MallocDebug, etc etc)

    For what its worth, my now deprecated NSDocumentController  override never gets called,
    whether I override the new method or not.  The documentation says that it should be called for binary compatibility, but
    it isn't.

    - (id)openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)outError
    {
      if ( outError )
          *outError = nil;

      NSLog( @"openDocumentWithContentsOfURL %@", absoluteURL);
      NSString *path = [absoluteURL path];

          MbDocument *currShow = [self mbdocument];
          NSString *currShowPath = [currShow filePath];
          NSInteger retVal;
          if ( !( [[MbEController instance] isFinishedLaunching] & kFinishedPreLaunchCheck) )
          {
            // user double-clicked a file to start Director
            currShowPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastSaveFile"];
            if ( [currShowPath isEqualToString: path] )
            {
                if ( [[[NSUserDefaults standardUserDefaults] objectForKey:@"isDocumentEdited"] boolValue] == YES )
                {
                  retVal = NSRunAlertPanel(@"Open Show File", @"Revert to Saved?", @"Revert to Saved", @"Keep Changes", NULL);
                  if ( retVal == NSAlertDefaultReturn )
                  {
                      [[MbEController instance] openShowFile:path];
                  }
                }
                return currShow; // proceed with normal startup
            }
          }


          if ( [currShowPath isEqualToString: path] )
          {
            NSLog( @"is edited %d currShow %@", [currShow isDocumentEdited], currShow );
            if ( [currShow isDocumentEdited] )
                [currShow revertDocumentToSaved:self]; // will prompt
            else // document is already open, so do what Apple's standard action is...
                [currShow showWindows];
          }
          else
          {
            if ( [currShow isDocumentEdited] )
                retVal = NSRunAlertPanel(@"Open Show File", [NSString stringWithFormat: @"The current show file has unsaved changes.  Discard unsaved changes and switch to show file '%@'?", path], @"Discard unsaved changes and switch to show", @"Keep Current", NULL);
            else
                retVal = NSRunAlertPanel(@"Switch to Show File", [NSString stringWithFormat: @"Switch to Show File '%@'?\n\nCurrent show file '%@'", path, currShowPath ? currShowPath : @"Untitled"], @"Switch", @"Keep Current", NULL);
            if ( retVal == NSAlertDefaultReturn )
                [[MbEController instance] openShowFile:path];
          }

          // user cancelled
          return currShow;

    }
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today