Skip navigation.
 
mlLoading project images in an IB Palette
FROM : Ricky Sharp
DATE : Sun Nov 28 06:57:08 2004

On Nov 27, 2004, at 11:25 PM, Ricky Sharp wrote:

> Right now I'm doing some trial-and-error stuff regarding loading
> images from a parent Xcode project.  While I do have code that can
> hunt down and load the images, such code isn't being invoked when my
> nib document opens.  I'm hoping to find a solution by listening to
> IBDidOpenDocumentNotification notifications.


Well, I got a working solution that knows how to deal with PDF files
(the code should ultimately work with any image type).

In my IBPalette subclass, I added these two methods:

+ (void)initialize
{
   [[NSNotificationCenter defaultCenter] addObserver:self
       selector:@selector(iiDocumentDidOpen:)
       name:IBDidOpenDocumentNotification object:nil];
}

+ (void)iiDocumentDidOpen:(NSNotification*)aNotification
{
   id <IBDocuments>    theDocument = [aNotification object];
   
   if (theDocument != nil)
       {
       id<IBProjects>    theProject = [theDocument project];
       
       if (theProject != nil)
           {
           NSArray*    resources = [theProject
filesForFileType:IBProjectResourcesFileType];
           
           if (resources != nil)
               {
               NSEnumerator*    enumerator;
               id                element;
               NSString*        elementString;
               NSRange            range;
               NSString*        nameKey = @".pdf";
               NSImage*        image = nil;
               
               enumerator = [resources objectEnumerator];
               
               while ((element = [enumerator nextObject]) != nil)
                   {
                   elementString = [element path];
                   range = [elementString rangeOfString:nameKey];
                   
                   if (range.location != NSNotFound)
                       {
                       image = [[NSImage alloc] initWithContentsOfFile:elementString];
                       
                       if (image != nil)
                           {
                           NSArray*    thePathComponents = [elementString pathComponents];
                           NSArray*    theFileComponents = [[thePathComponents lastObject]
componentsSeparatedByString:@"."];
                           
                           [image setName:[theFileComponents objectAtIndex:0]];
                           }
                       }
                   }
               }
           }
       }
}


Basically, whenever you double-click on a nib in your Xcode project,
this code is called when the IB document opens.  It then scans the
entire Xcode project's Resource folder for PDF files.  When found, an
NSImage is created and it is named.  The name I chose is the actual
file name sans the extension.

This now allows me to edit my custom widgets with images not contained
in the nib itself, but in the parent Xcode project.

___________________________________________________________
Ricky A. Sharp        mailto:<email_removed>
Instant Interactive(tm)  http://www.instantinteractive.com

Related mailsAuthorDate
No related mails found.