user1633930
user1633930

Reputation: 61

How do I implement an image array in Xcode?

I'm trying to create an array of images for an imageView and then change those images when a button is pressed. I'm just testing the ability for the button to cause a method to get and display the image from the array. What am I doing wrong?

This is the code I have so far:

- (void)viewDidLoad {

    NSMutableArray *imagesArray = [[NSMutableArray alloc] initWithObjects:@"Image1.JPG", @"Image2.JPG", nil];
}


-(IBAction)img1 {

    [imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:0]]];
}

I also get the warning 'unused variable 'imagesArray'

Thanks for helping!

Upvotes: 2

Views: 12510

Answers (2)

Andrei G.
Andrei G.

Reputation: 1740

you are not retaining NSMutableArray *imagesArray, it goes out of scope as soon as viewDidLoad completes.

try this:

declare instance variable NSMutableArray *imagesArray;

and init using

imagesArray = [[NSMutableArray alloc] initWithObjects:@"Image1.JPG", @"Image2.JPG", nil];

Upvotes: 2

Adam
Adam

Reputation: 26907

I guess you declared your array twice. Change your load method to this and try again.

- (void)viewDidLoad {

    imagesArray = [[NSMutableArray alloc] initWithObjects:@"Image1.JPG", @"Image2.JPG", nil];

}

Upvotes: 0

Related Questions