2012-01-15 2 views
2

私はNSMUtable配列の画像を持っていますが、別の画像が前と次のボタンで表示されますが、配列の終わりに到達するとシミュレータがクラッシュします。配列の終わりを最初にループして、次のボタンをもう一度押すと画​​像の配列の終わりに達したときに最初の画像に戻ります以前のボタンは、クラッシュなしの最後の画像にループします。ループ端から端までのuimimagesのNsmutable配列

答えて

2

円の配列は、標準NSMutableArrayを使用して簡単に実装できます。たとえば、あなたは配列にあなたのイメージを保存すると言うimageArrayと呼ばれるように、あなたの現在の画像のインデックスを追跡するために単純な変数を使用します。

int currentImageIndex = 0; 

...あなたはnextImagepreviousImageなどを実装するかもしれません。あなたは、配列をステップ実行したいとき

- (UIImage*) nextImage { 
    currentImageIndex = (currentImageIndex + 1) % [imageArray count]; 
    return [imageArray objectAtIndex:currentImageIndex]; 
} 

- (UIImage*) previousImage { 
    currentImageIndex--; 
    if (currentImageIndex < 0) { 
     currentImageIndex = [imageArray count] - 1; 
    } 

    return [imageArray objectAtIndex:currentImageIndex]; 
} 

は、それからちょうどnextImagepreviousImageを使用し、問題が解決しました。

0

シンプルな操作が可能です。あなたがする必要があるのは、あなたが最後の要素にいるかどうかを確認するチェックを作成し、そうであれば、カウントまたはiなどのトラッカーを0に設定することです。

psudoコード //セットインデックス

if (array [ index ] == len(array) - 1) //at end 
{ 
    index = 0 
} 

if(array [index] == -1)//at beginning 
{ 
    index = len(array) -1 
} 
// do something with array[index] 
0

私は、このソリューションを提案したい:

選択UIImage、現在のインデックスを保持するNSIntegerプロパティのプロパティ。

-(IBAction) nextButtonTapped:(id)sender 
{ 
    self.currentIndex = self.currentIndex++ % [self.images count]; 
    self.selectedImage= [self.images objectAtIndex:self.currentIndex]; 
    [self reloadImageView]; 
} 

-(IBAction) previousButtonTapped:(id)sender 
{ 
    self.currentIndex--; 
    if (self.currentIndex < 0) 
     self.currentIndex += [self.images count]; 
    self.selectedImage= [self.images objectAtIndex:self.currentIndex]; 
    [self reloadImageView]; 
} 

-(void)reloadImageView 
{ 
    //do, what is necessary to display new image. Animation? 

} 
関連する問題