I'm trying to change images in an app, but I want the iPhone to build the name of the object to change and then change it. Here's what I currently have but want to change...
It's possible, but it's not what you want here - you should use an array instead. You can add all of your object to an NSArray (or NSMutableArray if you need to modify the list) and access them like this:
Code:
//add imageViews to the array - do this once
NSArray *imageViews = [[NSArray alloc] initWithObjects: q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, nil];
//pull imageviews from the array
UIImageview *gradedImage = [imageViews objectAtIndex:currentQuestionNumber];
gradedImage.image = [imageViews imageNamed: imageLocation];
UIImageview *currrentImage = [myArray objectAtIndex:currentQuestionNumber+1];
currentImage.image = [UIImage imageNamed: currentAnswerImage];
It's possible, but it's not what you want here - you should use an array instead. You can add all of your object to an NSArray (or NSMutableArray if you need to modify the list) and access them like this:
Code:
//add imageViews to the array - do this once
NSArray *imageViews = [[NSArray alloc] initWithObjects: q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, nil];
//pull imageviews from the array
UIImageview *gradedImage = [imageViews objectAtIndex:currentQuestionNumber];
gradedImage.image = [imageViews imageNamed: imageLocation];
UIImageview *currrentImage = [myArray objectAtIndex:currentQuestionNumber+1];
currentImage.image = [UIImage imageNamed: currentAnswerImage];
I had to tweek the code a little but it's better than I got:
PHP Code:
//add imageViews to the array - do this once
NSArray *imageViews = [[NSArray alloc] initWithObjects: q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, nil];
//pull imageviews from the array
UIImageView *gradedImage = [imageViews objectAtIndex:currentQuestionNumber];
gradedImage.image = [UIImage imageNamed: imageLocation];
Sorry, I had some typos. Your fixed code looks good. Is it not working?
You can get variables by name using Key-Value coding, but it's *not* the right thing to do here - it's slow and it's overkill.
The fixed code is working great ( no errors or warnings ). I'm trying to make this method work in such a way where I don't need to hard code the objects in. Also, I'm originally a web programmer so I'm trying to associate how things are done with a framework vs. raw code.
If you still want to create the UIImageViews in Interface Builder but you don't want all of those vars / outlets, you can giver them all tags in IB and then locate them using [self.view viewWithTag:x] for adding to the array.
If you want to create them entirely in code, you can use initWithFrame: to create them , then add them as subviews in the current view and add them to the array.
More or less, I want to have a string called "q0" and I want to get the id for the UIImageView object named q0. Google is not my friend for this problem.
I can change an image like this:
PHP Code:
q0.image = [UIImage imageNamed: imageLocation];
but I would like to change the image like this sudo code:
Smasher is right - you should do this with arrays. Just create an array of imageview objects, and if you need a filename that matches the position of the array index, you can use stringWithFormat to generate it.