Clarence
Clarence

Reputation: 1943

Changing different image on UIImage with single button

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

Answers (1)

Rahul Vyas
Rahul Vyas

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

Related Questions