How to View Next Image in An Array Using Index/Integer - iPhone Dev
Right,
I've got an array which looks like so:
-(void) viewDidLoad;
{
imageArray = [[NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
nil] retain];
}
And I have an IBAction that is triggered when a button is clicked like so:
-(IBAction)next {
UIImage *img = [imageArray objectAtIndex:0];
[imageView setImage:img];
}
The problem is that when the button is clicked, it's only ever choosing the object at index 0 (obviously!). What I want to know is how do I define the index with an integer in the .h file, and use it in the IBAction so that whenever I click the next button it will go to the next image in the array rather than to index 0 every time?
Try this:
int current_image_index = 0;
-(IBAction)next {
if (current_image_index + 1 == [imageArray count])
current_image_index = 0;
UIImage *img = [imageArray objectAtIndex:current_image_index];
[imageView setImage:img];
current_image_index++;
}
精彩评论