我想使用一种 IBAction 方法,当点击时循环显示图像。即一个图像和一个按钮显示卸载,当用户点击按钮时,一个新图像显示在它的位置等。现在它只显示数组中的第一个图像。当我第二次点击按钮时,没有任何反应。有人可以帮我编辑这个以循环浏览我所有的图像吗?谢谢。
编辑
//when i tap the button only the first image shows in the array. What should I edit from the current app?
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Implementing our method
- (IBAction)buttonPressed
{
NSArray *imagesArray = @[@"image1.png",@"image2.png",@"image3.png",@"image4.png"];
count ++;
if(count == imagesArray.count)
{
count = 0;
}
yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
}
@end
最佳答案
试试这个....
将图像添加到您的项目中并将这些图像名称存储到 NSArray
例如。
//declare imagesArray as instance variable
NSArray *imagesArray = @[@"image1.png",@"image2.png",@"image3.png",@"image4.png"];
在按钮操作方法中
- (IBAction)buttonPressed
{
count ++;
if(count == imagesArray.count)
{
count = 0;
}
yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
}
关于ios - IBAction 循环浏览图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28870931/