0

I want to change the image in an imageview at the click of a button.

Sample code:

NSMutableArray *array = [[NSMutableArray alloc]init];

[array addObject:[UIImage imageNamed:@"pic1.png"]];
[array addObject:[UIImage imageNamed:@"pic2.png"]];
[array addObject:[UIImage imageNamed:@"pic3.png"]];

NSLog(@"%i" , [array count]);

for (int i = 0; i < [array count]; i++) {
    [type setImage:[array objectAtIndex:i]];
}

When I click the button, it displays pic3 and nothing else. Can anyone point me in the right direction?

Magnus
  • 57
  • 2
  • 4
  • What is `type`? What do you want to accomplish? – Ole Begemann Feb 21 '11 at 13:36
  • 1
    Please write more details: where is the button click code? what is this "type" object... etc. – Oleg Danu Feb 21 '11 at 13:38
  • Oh sorry. type is UIImageView, and the code i posted is -(IBAction)buttonClicked:(id)sender. I want to iterate through the array, when I click the button it shows the next image in the array. – Magnus Feb 21 '11 at 13:40
  • check this question for example - http://stackoverflow.com/questions/5042362/change-uilabel-in-loop/5042521 . it deals with UILabels but the principle is just the same – Vladimir Feb 21 '11 at 13:42

3 Answers3

1

you just set the image of the imageVIew to be pic1,then pic2 and then pic3.
if you want to change the image every click on button u should init ur array in ViewDidLoad, set int index=0;
and then, in the -(IBAction) u should inc the index and set the new pic, example:

index=(index+1)%[array count];
[type setImage:[array objectAtIndex:index]];
Ratinho
  • 298
  • 1
  • 6
0

Use this:

- (IBAction)buttonClicked:(id)sender
{
   static int i = 0; 
   if(i == 3)
     i = 0;
   i++;
   [type setImage:[array objectAtIndex:i]];
}

Now connect this method to your button action and type to your imageView.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
Amit Singh
  • 8,383
  • 4
  • 28
  • 31
  • Done! It works, only problem with this is, the index reaches 3 and then the app terminates. EDIT: Nevermind, I figured it out. Just put if(i == 3) i = 0; Thanks a lot! – Magnus Feb 21 '11 at 13:52
  • @Magnus - Welcome to Stackoverflow! What you want to do next is hit the check box at the top of Amit's answer (right under the up and down arrows) to "accept" this answer. That'll give Amit credit for correctly answering your question, and you'll get some karma too, meanwhile we built a community-driven database of quality questions and answers. – Dan Ray Feb 21 '11 at 14:38
  • How can I do the opposite of this? I added another button, which is going to iterate through the images, only in the opposite direction. Like back and forth – Magnus Feb 21 '11 at 15:52
  • @Magnus: obviously, for the other direction you have to decrement `i` (`i--` instead of `i++`) and reverse the "round trip" to go from 0 to 3. Why don't you try it for yourself? – Ole Begemann Feb 21 '11 at 18:12
0

There is no delay in between setting images1, 2, and 3, so you only ever see 3.

Bogatyr
  • 19,255
  • 7
  • 59
  • 72