我試圖通過單擊按鈕向後循環數組。向後循環通過NSArray
我目前的代碼很接近,但並不完美。
- (void)viewDidLoad {
self.imageNames = [NSArray arrayWithObjects:@"MyFirstImage", @"AnotherImage", nil];
currentImageIndex = 0;
[super viewDidLoad];
}
...是什麼在起作用:
- (IBAction)change {
UIImage* imageToShow = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[imageNames objectAtIndex:currentImageIndex] ofType:@"png"];
currentImageIndex++;
if (currentImageIndex >= imageNames.count) {
currentImageIndex = 0;
}
}
...,什麼是不工作:
- (IBAction)changeBack {
UIImage* imageToShow = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[imageNames objectAtIndex:currentImageIndex] ofType:@"png"];
currentImageIndex--;
if (currentImageIndex >= imageNames.count) {
currentImageIndex = 0;
}
}
任何幫助,很高興地感謝!
謝謝!
謝謝!這就像一個魅力。 – dot 2010-06-14 23:34:53