views:

133

answers:

2

Hey All,

I was wondering what I should use to display an image on screen every time the user presses a button. I am using Objective-C/CocoaTouch on the iPod Touch. I would like to pull these images from an array I have set up and place them on the screen when the button is triggered. I feel dumb asking but any one that can point me in the right direction would be great. I think I could figure out the rest from there.

Thanks.

+2  A: 

I'm sure there are many ways of doing this, but one way would be to define a UIImageView on the screen, and make it hidden. When the user presses the button, you can set the source of the UIImageView to the image from your array, and set the hidden property to NO.

Brian Yarger
I get the hidden property set to NO thing but how do I set the source of the UIImageView if the image is going to be changing every time they press the button? I think I could set a variable up, but the actual code for this is escaping me.
I00I
+2  A: 

I think it's as simple as something like this:

[myImageView setImage:[imageArray objectAtIndex:theIndex]];

In your button's action method. You can set theIndex before or afterwards to get a different image from your array every time the action method gets called.

Carl Norum
Okay I think this is what I was confusing myself with. Instead of changing the index for the array on the array change it from the button is what you are saying? How can I change the index if I have to set it in the above code snippet? Just set it to "i" or something?
I00I
Well, you could make the current index an instance variable of your controller class (the one implementing the button action method). Then in the button action you can do `theIndex++` or whatever other algorithm you want to use to specify the image index. If it's complicated, just write a method like `- (void)nextImageIndex` and put the implementation in there.
Carl Norum