Aaron Navies
Aaron Navies

Reputation: 1263

IBAction cycle through images on tap

I want to use an IBAction method that when on tap cycles through images. Ie one image and a button is shown unload and when the user taps the button a new image is shown in its place etc. Right now it just shows the first image in the array. When I tap the button a second time nothing happens. Can someone please help me edit this to cycle through all of my images? Thanks.

EDIT

//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

Upvotes: 0

Views: 210

Answers (1)

Ravi
Ravi

Reputation: 2451

try this....

add images into your project and store those images names into an NSArray

for ex.

//declare imagesArray as instance variable

NSArray *imagesArray = @[@"image1.png",@"image2.png",@"image3.png",@"image4.png"];

and in the button action method

 - (IBAction)buttonPressed 
 { 
          count ++;

          if(count == imagesArray.count)
          {
           count = 0;
          }

          yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
 }

Upvotes: 1

Related Questions