Take the 2-minute tour ×

Basically I need a button that switches between 3 images,I am not sure how I can accomplish this and I have been trying to look for a way.

share|improve this question

closed as not a real question by Josh Caswell, Perception, Linger, The Shift Exchange, dreamcrash Jan 7 '13 at 3:48


Are you looking for a button that switches images each time you press it? –  AgnosticDev Jan 6 '13 at 20:03
are you talking about a normal, hover and pressed image for the button? –  Pedro Vieira Jan 6 '13 at 20:04
Not exactly, I need a button that switches between 3 images each time user presses it. –  moo Jan 6 '13 at 20:04
For example I push a button it goes to a picture of an apple, than an orange than a tomato. –  moo Jan 6 '13 at 20:05
Instead of storing the current index in an instance variable; you can make a call to the indexOfObject to the array with the image that is already displayed; –  Siby Jan 6 '13 at 20:13

2 Answers 2

What you need is an array and modulo arithmetic:

NSArray *images = [NSArray arrayWithObjects:image1, image2, image3, nil];

int idx = 0;

- (void)buttonPressed:(UIButton *)sender
    imageView.image = [images objectAtIndex:idx];
    idx = (idx + 1) % images.count;
share|improve this answer

In viewDidLoad you could take an UIImageView and add it to the view. Then take an NSArray of UIImages and add the image at index 0 to the UIImageView as it's default image. Then create a custom button and every time it is clicked it will rotate to a new image in the NSArray. Does this help?

share|improve this answer
