I know that I can do this:
switch (imageNumber) {
case 1: image1.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
case 2: image2.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
case 3: image3.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
case 4: image4.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
}
I want to be more efficient in my code, so I am wondering if there is a way to do it like this:
switch (imageNumber) {
case 1: //somehow set image1 as the imageView I want used
case 2: //somehow set image2 as the imageView I want used
case 3: //somehow set image3 as the imageView I want used
case 4: //somehow set image4 as the imageView I want used
}
imageWhicheverWasSet.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]];
Thanks in advance for your help!
The second code snippet you pasted yourself is actually very close to a solution. You just need to set
UIImageView *imageToChange;as instance variable and then do: