Reputation: 57
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?
Upvotes: 0
Views: 6041
Reputation: 8383
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.
Upvotes: 0
Reputation: 19323
There is no delay in between setting images1, 2, and 3, so you only ever see 3.
Upvotes: 0
Reputation: 298
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]];
Upvotes: 1