loading an image referenced in an array
Is it possible to store an image name in an array then have a UIImage load开发者_JS百科 this image through referencing the array something like this
[imageView setImage:[UIImage imageNamed:[quizArray objectAtIndex:1]]];
the image is in the resource folder
thanks Chris
ps ... beginner
Yep, it is possible. Additionaly, your code is correct too. App should load the second image in your array (indexes begin from 0).
If you're a beginner you should look for some books or online tutorials for iOS programmers. For example, lynnda.com offers you some video tutorials. (I used one of them when I was a beginner too) :)
Absolutely possible. However, when you say the image is in the resource folder
, I'm assuming that means that you just want to get an image from your resources folder, then display it, correct?
If so, there's no need to stuff it in an array. Just access it like this:
[imageView setImage:[UIImage imageNamed:@"someimage"]];
However, in your example you used [quizArray objectAtIndex:1]
in combination with imageNamed:
. Remember, imageNamed:
only takes a string parameter, so passing an object to it will crash your application.
I would arrange your question in a different way. Instead of using arrays, you'd be better off with using dictionaries. Like this:
NSImage *image1 = [NSImage imageNamed:@"myimage"];
NSMutableDictionary *quizDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"What colour is the sky?","question",
@"blue",@"answer",
@"red",@"wrong1",
@"green",@"wrong2",
@"yellow",@"wrong3",
image1,@"image",nil];
Then to access it, you can just use the key names, like this:
NSString *question = [quizDict objectForKey:@"question"];
NSString *correct = [quizDict objectForKey:@"answer"];
NSImage *image = [quizDict objectForKey:@"image"];
精彩评论