Image Array IBAction用于查看数组中的下一个图像 [英] Image Array IBAction to view next image in the array

查看:58
本文介绍了Image Array IBAction用于查看数组中的下一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢@JFoulkes我的应用程序在单击下一个按钮时显示数组中的第一个图像。但是,当我再次单击它时,它不会显示数组中的下一个图像。这让我相信我的IBAction下一个按钮不太正确。

Thanks to @JFoulkes I have got my app to show the first image in the array when clicking the next button. However, when I click it again, it does not show the next image in the array. This leads me to believe my IBAction for the next button is not quite right.

这是我到目前为止的代码...

Here's the code I have so far...

在我的.h文件中我声明:

In my .h file I have declared:

IBOutlet UIImageView *imageView;
NSArray *imageArray;
NSInteger currentImage;

我还补充说:

-(IBAction) next;

在.m文件中我有:

-(void) viewDidLoad;
{
imageArray = [[NSArray arrayWithObjects: 
                [UIImage imageNamed:@"1.png"], 
                  [UIImage imageNamed:@"2.png"], 
                  nil] retain];
}

这是我的IBAction,它只显示数组中的第一个图像( 1.png)再次点击时不会将UIImageView更改为第二个数组图像(2.png):

This is my IBAction which is only ever showing the first image in the array (1.png) and when clicked again doesn't change the UIImageView to the second array image (2.png):

-(IBAction)next {

 if (currentImage + 1 == [imageArray count])
        currentImage = 0;
    UIImage *img = [imageArray objectAtIndex:currentImage];
    [imageView setImage:img]; 
   currentImage++;
}

根据代码,如何更改IBAction以成功迭代点击之后数组中的图像?

Based on the code, how can I change the IBAction to successully iterate through the images in the array after ever click?

推荐答案

你的递增逻辑被破坏了,所以第一次点击什么都不做,你永远不会到达最后一张图片。如果要在数组中添加3.png,这将更加明显。逐步完成代码,观察它在每一步的作用可能是有益的。

Your incrementing logic is broken, so the first click will do nothing and you can never reach the last image. If you would add a 3.png to your array, this would be a little more obvious. It might be instructive to step through the code, watching what it does at each step.

正确的递增逻辑看起来像这样:

Correct incrementing logic would look something like this:

- (void)next {
    currentImage++;
    if (currentImage >= imageArray.count) currentImage = 0;
    UIImage *img = [imageArray objectAtIndex:currentImage];
    [imageView setImage:img];
}

这篇关于Image Array IBAction用于查看数组中的下一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆