开发者

Interface Builder: Choose media based on target

开发者 https://www.devze.com 2023-01-23 16:54 出处:网络
In Xcode I use targets to release several apps versions which share some base code and tend to have different graphics.

In Xcode I use targets to release several apps versions which share some base code and tend to have different graphics.

i.e. Pretending I have target JOHN and target DOE. I could add different images named exactly example.png to the bundle, and have each one targeted respectively. The code does not need any changes this way.

UIImageView *image = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"example.png"]];
开发者_如何学JAVA

If instead I named each image per different version I would end up with something similar to this:

NSString *imageName;

#ifdef JOHN
imageName = @"johnExample.png";
#else
imageName = @"doeExample.png";
#endif
UIImageView *image = [UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

Having several versions and many images this quickly becomes messy and having a huge file with constants looks to me like an overkill, so I end up with the first option.

The problem is:

While working on nibs files, Interface Builder just appears to load example.png randomly. So if I'm working on target JOHN it's an annoyance that IB just decided to load example.png from the DOE target.

There's any way for Interface Builder to load media based on a target?


Just to close this question, the answer is: no.

As of Jan '11, you can't load media based on a target on Interface Builder, but you could use some workarounds, as Digital Robot and my own description in the question pointed out.


why not have this define at the beginning of the code

#ifdef JOHN
#define APP @"john"
#elseif DOE
#define APP @"doe"
#endif

and then, every time you want to use this, you may have...

imagename = [NSString stringWithFormat:@"%@Example.png", APP];


That should work. This may be new because some time has passed and I just learned that recently. Don't know wether this was possible ages ago when you acutally asked this question :) But it may be useful for others searching for a solution.

When adding a target to a project within xcode then it creates a subfolder in the file system. There is one subfolder for each of the taragts. The basic idea is, as far as I understand it, that all commonly used files are on the upper level in the project main directory and those that are individual to each target are in their respective directory.

(A bit like localized versions of files.)

So you may put the image of John as example.png in /project/john and the image of Doe as example.png in /project/doe. You should double check that the correct versions of each image are included in the build settings for each of the targets respectively. (Copy Boundle Resources) Doing so "example.png" is included only once in the boundle. Randomly loading of one image or the other should not longer happen, although you refer to it as "example.jpg" within your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消