Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dakotas

macrumors newbie
Original poster
Apr 11, 2011
2
0
Hi, I have ten pictures and I want to have two buttons, previous and next so i can click through to view the pictures. I am finding tutorials on animation which is not what i want to achieve. can anyone help me?
 

nickculbertson

macrumors regular
Nov 19, 2010
226
0
Nashville, TN
do something like this.
(images is an ImageView)

Code:
-(IBAction) right{
UIImage *Image1 = [UIImage imageNamed:@"yourpic1.png"];
UIImage *Image2 = [UIImage imageNamed:@"yourpic2.png"];
UIImage *Image3 = [UIImage imageNamed:@"yourpic3.png"];



                        if(images.image == Image1)
				images.image = Image2;
			else if(images.image == Image2)
				images.image = Image3;
                        else if(images.image == Image3)
				images.image = Image1;
}

-(IBAction) left{
UIImage *Image1 = [UIImage imageNamed:@"yourpic1.png"];
UIImage *Image2 = [UIImage imageNamed:@"yourpic2.png"];
UIImage *Image3 = [UIImage imageNamed:@"yourpic3.png"];



                        if(images.image == Image3)
				images.image = Image2;
			else if(images.image == Image2)
				images.image = Image1;
                        else if(images.image == Image1)
				images.image = Image3;
}

Nick
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Or you could consider preloading your images into an array and then just using indexes to reference them. Something like this:
Code:
NSArray *imageArray;
int imageArrayIndex;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    imageArray = [[NSArray alloc] initWithObjects:
        [UIImage imageNamed:@"yourpic1.png"],
        [UIImage imageNamed:@"yourpic2.png"],
        [UIImage imageNamed:@"yourpic3.png"],
        nil];

    imageArrayIndex = 0;
}

- (IBAction)right {
    imageArrayIndex++;
    if (imageArrayIndex == imageArray.count) {
        imageArrayIndex = 0;
    }
    images.image = [imageArray objectAtIndex:imageArrayIndex];
}

- (IBAction)left {
    imageArrayIndex--;
    if (imageArrayIndex == -1) {
        imageArrayIndex = imageArray.count - 1;
    }
    images.image = [imageArray objectAtIndex:imageArrayIndex];
}

That way if you add or remove images from the array, the only code you'd need to change would be in viewDidLoad.

P.S. I've left the release of imageArray as an exercise for the reader. :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.