2

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!

1
  • 1
    'unused variable "imagesArray"'.. Think twice. You probably haven't used it yet... Commented Aug 29, 2012 at 17:30

2 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.