views:

69

answers:

2

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];
}
A: 

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.

Lou Franco
+1  A: 

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];
}
TomH