Upvote:2

Aprove answer
@interface ViewController ()

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) NSArray *imageNames;
@property (assign, nonatomic) int currentImageIndex;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.imageNames = @[@"img1", @"img2", @"img3", @"img4"];
    self.currentImageIndex = -1;
}

- (void)changeImage {
    if (++self.currentImageIndex == self.imageNames.count) {
        self.currentImageIndex = 0;
    }
    self.imageView.image = [UIImage imageNamed:self.imageNames[self.currentImageIndex]];
}

@end

Upvote:1

- (IBAction)changeImage:(id)sender {

    if (!self.userImageView.image || 
        [self.currentDisplayedImageString isEqualToString:@"img40.png"]) {
        self.currentDisplayedImageString = @"img1.png";
    }
    else {
        // Get the filename's numerical index by parsing out the numerical component
        NSString *index = [[self.currentDisplayedImageString componentsSeparatedByCharactersInSet:
                            [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                           componentsJoinedByString:@""];
        // "Increment" the currentDisplayedImageString
        self.currentDisplayedImageString = [NSString stringWithFormat:@"img%@.png", index];
    }

    // Then update the image
    UIImage *image = [UIImage imageNamed:currentDisplayedImageString];
    self.userImageView.image = image;

}

Credit Goes to: stackoverflow.com

Related question with same questions but different answers