0

Possible Duplicate:
access to array object

i have an array that contain images and an imageview in my xib file. now, i want to view in my imageview the first object(image) that my array contain. i tried to do it for couple hours but i didn't succeed. how can i do it??

mt array deceleration:

    photoArray = [[NSMutableArray alloc] init];
    PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest"  photographer:@"roy"];
    PhotoItem *photo2 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"2.jpg"] name:@"roy's hand" photographer:@"roy"];
    PhotoItem *photo3 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"3.jpg"] name:@"sapir first" photographer:@"sapir"];
    PhotoItem *photo4 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"4.jpg"] name:@"sapir second" photographer:@"sapir"];
    [photoArray addObject:photo1];
    [photoArray addObject:photo2];
    [photoArray addObject:photo3];
    [photoArray addObject:photo4];

the code that i tried to work with:

imgView = [[UIImageView alloc] initWithImage:[photoArray objectAtIndex:0]];

thanks!!

Community
  • 1
  • 1
APRULE
  • 105
  • 3
  • 11
  • I see you asked a very similar question which I answered. http://stackoverflow.com/questions/10864893/access-to-array-object/10868444#10868444 You didn't accept that one either. Buy a book on Objective-C/iOS and read them. You won't need to ask questions like this and it would improve your understanding of coding greatly! – Benjamin Jun 03 '12 at 20:39

2 Answers2

1

The answer to your problem is very simple try to do it in steps if you are new to all this.

array: photoarray 
imageView in xib : imgView
imgView =[UIImageView alloc]init];
imgView.image=[UIImage imageNamed:[photoarray objectatindex:0]];

This will serve the purpose of your question.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Deep Batra
  • 112
  • 5
0

Your photos array doesn't contain images, it contains custom PhotoItem objects. You need to grab the UIImage from that object. I'm assuming you have a property set maybe named photo on PhotoItem. You need to call this.

imgView = [[UIImageView alloc] initWithImage:[[photoArray objectAtIndex:0] photo]];

// or

imgView = [[UIImageView alloc] initWithImage:[photoArray objectAtIndex:0].photo];

UPDATE

You have to have properties to access information about an object. You need to rebuild your PhotoItem Class to have properties for the things you want to access like photo, name, and photographer. Instance Variables can't be accessed the way you're trying.

// PhotoItem.m

@interface PhotoItem : NSObject
{
     // Instance Variables aren't accessible outside of the class.
}

@property (nonatomic, strong) UIImage *photo;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *photographer;

- (id) initWithPhoto:(UIImage)image name:(NSString*)stringA photographer:(NSString*)stringB;

And you need to rewrite you init probably too.

// PhotoItem.h

@implementation PhotoItem

@synthesize photo, name, photographer;

- (id) initWithPhoto:(UIImage)image name:(NSString*)stringA photographer:(NSString*)stringB
{
    self = [super init];
    if (self) {
         photo = image;
         name = stringA;
         photographer = stringB;
    }
}

Then to access anything its

PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest"  photographer:@"roy"];

photo1.photo // returns UIImage
photo1.name // returns NSString
photo1.photographer // returns NSString

Therefore if they are objects in an array it would be

[photoArray objectAtIndex:0] // returns your PhotoItem Object
[[photoArray objectAtIndex:0] photo] // returns that PhotoItem Object then gets the photo out of it returning a UIImage in total.
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98