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

4

9

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?

Deafening answered 8/9, 2011 at 6:16 Comment(2)
Did you remember to assign them to their different targets?Jezabella
Yes (I assigned the blue folders on creation. I can't seem to assign individual files inside)... As it seems, IB can't really get to those blue folders. I managed to do it with #IFDEF inside the CustomeCell class, pointing to the appropriate blue folder, but that would mean a lot of these #IFDEFs are needed, and I was hoping for a way to do it via IB instead of adding lots of IFs... If anyone knows of a way, it would be appreciated. Thanks.Deafening
A
3

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.

Affect answered 1/7, 2013 at 7:40 Comment(0)
C
2

You could try two copies of .xib, each a member of different target. .m and .h can be members of both targets, if you wish (so only .xib is duplicated).

I just posted a similiar question which might help you with this.

Corruptible answered 16/1, 2012 at 10:22 Comment(0)
J
0

I've had problems with the blue folders, I'm not sure how they are supposed to work!

I use separate targets for a free and paid version of my app, and this is how I separate the different icon files (troublesome because they have to have certain names)

My file system looks like this:

Resources

IconsPaid

Icon.png

[email protected]

Etc.

IconsFree

Icon.png

Etc.

I then drag the IconsPaid and IconsFree folders to XCode and assign the contents to the relevant targets

Jezabella answered 8/9, 2011 at 8:14 Comment(0)
A
0

I was having the same issue. I managed to pass from one target to another by renaming the folder containing the images of the target I wanted to be shown so that it becomes the first in alphabetical order.

Aulos answered 21/6, 2019 at 22:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.