开发者

How to View Next Image in An Array Using Index/Integer - iPhone Dev

开发者 https://www.devze.com 2023-02-19 16:21 出处:网络
开发者_StackOverflow中文版Right, I\'ve got an array which looks like so: -(void) viewDidLoad; { imageArray = [[NSArray arrayWithObjects:
开发者_StackOverflow中文版

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++;



}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号