Cycle backwards through NSArray

Posted by dot on Stack Overflow See other posts from Stack Overflow or by dot
Published on 2010-06-13T02:32:12Z Indexed on 2010/06/13 2:42 UTC
Read the original article Hit count: 224

Filed under:
|

Hello!

I'm trying to cycle backwards through an array by clicking a button.

My current code is close, but doesn't quite work.

- (void)viewDidLoad {
self.imageNames = [NSArray arrayWithObjects:@"MyFirstImage", @"AnotherImage", nil];
currentImageIndex = 0;
[super viewDidLoad];
}

...what works:

- (IBAction)change {
UIImage* imageToShow = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[imageNames objectAtIndex:currentImageIndex] ofType:@"png"];
currentImageIndex++;
if (currentImageIndex >= imageNames.count) {
    currentImageIndex = 0;
}
}

...and what isn't working:

- (IBAction)changeBack {
UIImage* imageToShow = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[imageNames objectAtIndex:currentImageIndex] ofType:@"png"];
currentImageIndex--;
if (currentImageIndex >= imageNames.count) {
    currentImageIndex = 0;
}
}

Any help is gladly appreciated!

Thanks!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk