• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Handling Multiple Document Types
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handling Multiple Document Types


  • Subject: Re: Handling Multiple Document Types
  • From: acoolie <email@hidden>
  • Date: Mon, 9 Jul 2007 17:58:01 -0400

Thank you all for the ideas. I used David's idea, since it looked like the
simplest code. It did take a little extra thought though. What I ended up
using is I set the default document type name for my file extension to that
a generic name for a new document. Then in -typeForContentsOfURL:error: I
returned a name for if there is actually a file to handle. In
documentClassForType: I did a simple check for each name and returned the
class. Re-reading your email a second time, it looks more obvious that this
is the solution you meant.



On 7/9/07, David Phillip Oster <email@hidden> wrote:

At 6:35 AM -0700 7/9/07, email@hidden wrote: >I have a NSDocument application which should handle two types of document. >One document will be an actual file and the other will not. My current >working solution is to determine which type of document the document is in >MyDocument, storing it in an instance variable, then using a simple if-else >in all methods to run different code for each document type.

You can create a subclass of NSDocumentController , instantiate it in
your MainMenu.nib, and give it the following overrides:

@implementation MyDocumentController
- (NSString *)typeForContentsOfURL:(NSURL *)inAbsoluteURL
error:(NSError **)outError {
   if ([... my predicate here...:inAbsoluteURL]) {
     return @"MyDocument1";
   } else if ([... my predicate2 here...:inAbsoluteURL]) {
     return @"MyDocument2";
   }
   return nil;
}

- (Class)documentClassForType:(NSString *)typeName {
   if ([... my predicate here...:typeName]) {
     return [MyDocument1 class];
   } else if ([... my predicate2 here...:typeName]) {
     return [MyDocument2 class];
   }
   return nil;
}


@end

documentClassForType: will be called for making new, untitled
documents, while typeForContentsOfURL: will be called for opening
existing files, etc.
_______________________________________________

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


  • Prev by Date: Proper way to load child Window/Controller?
  • Next by Date: Re: Modern way of getting the list of running applications?
  • Previous by thread: Re: Handling Multiple Document Types
  • Next by thread: Multi-project solution in Xcode
  • Index(es):
    • Date
    • Thread