Search code examples
xcoderesourcesxcode4uiimageviewtarget

Xcode 4: Multiple Targets, Multiple Resources Folders with images of the same name to use with a shared Xib?


I have made an app with a certain functionality and a certain graphical look.

For example, a custom tableview cell is loaded from a Xib file and has a UIImageView as a background image, loaded with a Pink image ("customcell.png") from the resources group.

Now, I have duplicated the original Target to create a blue version of the app. Also, the new Target is using its own Info.plist file with its own bundle identifier.

I wanted the UIImageView inside the Xib file to use the same filename ("customcell.png"), but take a the blue version of customcell.png if the blue Target was built.

I know you can't have files with the same name in a project, even if you put it in a group (yellow folder), since that's just a logical folder.

So I created 2 blue folders (called "blue", and "pink"), each associated with the correct Target. I have put a blue version of "customcell.png" in the "blue" folder, and a pink version of "customcell.png" in the "pink" folder.

I have deleted the original "customcell.png" from the project, leaving just the 2 copies that are in the new folders.

In Interface Builder, the UIImageView has gone blank (since I deleted the original image file), so I selected "customcell.png" again from the dropdown list. It showed only 1 file of that name (not 2), and it is the blue one (don't know why).

However, when I build to any of the targets, I get an empty UIImageView and a warning:

Could not load the "customcell.png" image referenced from a nib in the bundle with identifier "com.myapp.blueversion"

(or pinkversion)

So, basically my question is, how can I use different images with the same name, associated to different targets, but having 1 Xib file that loads them appropriately?


Solution

  • The problem you're having is that you're trying to use the blue folders as if they were normal file system folders. The blue folders are actually just groups of files for organization in the IDE and don't represent a folder hierarchy 'under the hood'.

    Take a look at how it works by right-clicking on some of the images to view them in Finder and you'll notice that files in 2 different blue group folders are actually in the same directory by default.

    How to use one xib referencing the same named images for multiple targets:

    1. In Finder, create a new folder in the same directory where the AppDelegate is found, probably naming it after your target.
    2. Store all your images for that target in that folder.
    3. Right-click the project's main group/folder in the top left pane and use "Add Files to 'XcodeProjectName'…".
    4. In the appearing upper pane, find and select the folder you just created on the file system.
    5. You can leave the Destination box checked or unchecked, since the files already exist at the location you specified.
    6. Keep 'Create groups for any added folders' selected to create a blue group folders.
    7. Make sure your target is checked.

    Just repeat this process for each target.

    When you view the xib in Xcode it will always show one version of the image, but it will be correct when you run the app.