Changing image of a view

Discussion in 'iOS Programming' started by Nraygun, Jan 29, 2009.

  1. Nraygun macrumors newbie

    May 9, 2005
    I loaded a number of images into an array in a ViewDidLoad method:
    myImagesArray = [NSArray arrayWithObjects: [UIImage imageNamed:mad:"image1.jpg"], [UIImage imageNamed:mad:"image2.jpg"], etc., nil];

    If I set the image of my view in this same method with this:
    myImageView.image = [myImagesArray objectAtIndex:1];

    it works.

    If I do this last statement elsewhere in the same object I get a crash:
    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (0)'

    If it works in one area(inside of ViewDidLoad) why doesn't it work in another area? I thought the scope of variables was across all methods in a class.

    What am I doing wrong?
  2. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    Is the array retained in any way? Arrays created by any method other than alloc/new/copy/mutable copy are all autoreleased and without retaining them they are prone to vanishing at any time.
  3. Nraygun thread starter macrumors newbie

    May 9, 2005
    That's probably it.

    So I just need to throw one of these in, right?

    [myImagesArray retain];

Share This Page