How do I implement an image array in Xcode?

12,441

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];
Share:
12,441
user1633930
Author by

user1633930

Updated on June 04, 2022

Comments

  • user1633930
    user1633930 almost 2 years

    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!

  • user1633930
    user1633930 over 11 years
    Problem fixed. Thanks for the help!