acdesign
acdesign

Reputation: 31

Button to populate image from array

Hey guys i have an image view, with a popover controller. There is an array called detailitem which loads images into the image view. All i want is a button to load next image in array into the image view how would i write this? Here is my attempt it doesn't work?

-(IBAction) nextitem {
    NSString * imageName = [NSString stringWithFormat:@"%@.jpg",[detailItem objectAtIndex:+1]];
    imageview.image = [UIImage imageNamed:imageName];
}

Upvotes: 1

Views: 336

Answers (2)

TomH
TomH

Reputation: 2975

You need to keep track of which index in the array is currently begin displayed.

Add an imageIndex instance variable to your class and initialize it to 0

-(IBAction) nextitem {
    imageIndex++;
    if(imageIndex == [detailItem count]) imageIndex = 0; // don't overrun the end of the array
    NSString * imageName = [NSString stringWithFormat:@"%@.jpg",[detailItem objectAtIndex:imageIndex]];
    imageview.image = [UIImage imageNamed:imageName];
}

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89192

What do you expect

  [detailItem objectAtIndex:+1]

To do? It will always pass 1 and get the 2nd element of the array. If you want to loop through the array, you need to keep an index somewhere (in a property, for example) and increment it.

Upvotes: 0

Related Questions