maniclorn
maniclorn

Reputation: 1089

appending string to file name by passing through variable in objective c

NSString *devicetype;       

if((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)){  
devicetype = @"ipad";  
}else{    
devicetype = @"iphone";  
}

I have two images

BACKGROUND_IPAD.PNG

AND

BACKGROUND_IPHONE.PNG

Now I want to use this string like

BackGround = [CCSprite spriteWithFile:[NSString StringWithFormat:@"background_%@.png",DEVICETYPE]];

Is this possible? If not, then is there a better way?

Upvotes: 0

Views: 107

Answers (2)

maniclorn
maniclorn

Reputation: 1089

Yes it works. I have tested and running successfully.

Upvotes: 0

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

Yes you an use it, but insread of that you can change the name of the image to the following

Image~ipad.png 
Image~iphone.png 

Now when you load the image

//on the iPhone and iPad use the same image name
//image named will select the image automatically
UIImage *image = [UIImage imageNamed:"Image"];

It will automatically select the image basing on your current device

Upvotes: 4

Related Questions