Reputation: 1943
I am trying to code a UIButton that can change a list of image in a UIImageView. The codes are listed below.However, after changing all the images and reached the last one, the button needs to be click twice in order to get back to the first image. May I know how I can modify it?
Thanks All.
-(IBAction)changeimage:(id)sender{
if (currentimage ==0) {
NSString *strTemp = [[NSString alloc]initWithString:@"stamp.png"];
currentimage =1;
aImage.image=[UIImage imageNamed:strTemp];
}
else if (currentimage ==1) {
NSString *strTemp = [[NSString alloc]initWithString:@"yoda.jpg"];
currentimage =2;
aImage.image=[UIImage imageNamed:strTemp];
}
else if (currentimage ==2) {
NSString *strTemp = [[NSString alloc]initWithString:@"44cents.jpg"];
currentimage =3;
aImage.image=[UIImage imageNamed:strTemp];
}
else (currentstamp =0);
}
Upvotes: 0
Views: 1332
Reputation: 28720
Very Simple
-(IBAction)changeimage:(id)sender{
if(currentimage == 3){
currentimage = 0;
}
if (currentimage ==0) {
NSString *strTemp = [[NSString alloc]initWithString:@"stamp.png"];
currentimage =1;
aImage.image=[UIImage imageNamed:strTemp];
}
else if (currentimage ==1) {
NSString *strTemp = [[NSString alloc]initWithString:@"yoda.jpg"];
currentimage =2;
aImage.image=[UIImage imageNamed:strTemp];
}
else if (currentimage ==2) {
NSString *strTemp = [[NSString alloc]initWithString:@"44cents.jpg"];
currentimage =3;
aImage.image=[UIImage imageNamed:strTemp];
}
Upvotes: 1