Got Code To View Next Image, How to Change To View Previous?
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 imageCo开发者_Go百科unt=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;
}
I think the problem is due to the if statement, which checks for index <=0 instead of >=0. Try this
- (IBAction)prev {
// <-- here
index--;
// Set imageCount to as many images as are available
int imageCount = 30;
if (index>=0)
{
NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
[picture setImage: [UIImage imageNamed: imageName]];
}
}
Note: index has to be a ivar or global variable
精彩评论