You have a code to view the next image, how to change the previous view?

advertisements

I have the code below and this allows me to click a button and it switches to the next image, how to I make this so that when I click another button it goes to the previous image?

- (IBAction)next {
static int index = 0;  // <-- here
    index++;
    // Set imageCount to as many images as are available
    int imageCount=31;
    if (index<=imageCount) {
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]];
    }
}

Thanks.

I now have this code:

- (IBAction)prev {
      // <-- here
    index--;
    // Set imageCount to as many images as are available
    int imageCount=3;
    if (index<=0) {
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]];
    }
}

but it doesn't work, it just changes the imageview to a blank one :'(

It also returns a warning: unused variable imageCount.

I have implemented a ivar now.

Please help thanks


I think you need to make index either a global var or an ivar of the view:

Create an Ivar:

@interface MyView : UIView {
       int index;
}
@property (nonatomic) int index;

@implementation MyView {
@synthesize index;
}