views:

826

answers:

1

I'm trying to add multiple instances of UIImageView(s) to an NSMutableArray of NSMutableDictionar(ies).

I am doing this, to be able to go back and pull out the UIImageView reference, to change the position of these images.

It seems that, when I pull the info out of the Dictionary, that it only returns a null pointer...

Any suggestions on how I should implement this? There may be a way easier method to store a list of n images and be able to reference them later....

// (for i = 0 to 30)

UIImageView *newBall = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];
newBall.center = CGPointMake (arc4random() % 280 + 20, arc4random() % 440 + 20);
[self.view addSubview:newBall];

NSMutableDictionary *dictBall = [[[NSMutableDictionary alloc] init] autorelease];
[dictBall setValue:newBall forKey:@"imgBall"];

// the dictionary is then added to the array

[newBall release];

// end for loop

(I'm using a Dictionary, because I also store a dy/dx value for rate of change in movement)

+1  A: 

Code above doesn't look OK to me.

You're creating a new NSMutableDictionary *dictBall everytime through the loop. Is something retaining it after the loop exists? Doesn't look like it from your code. This may be why your object is nil.

Move this line outside/before the for loop, and you should be fine.

Also, are you sure you want to store UIViewImage in a dictionary? Why not just an ID (based on the tag) and the coordinates (CGPoint or CGRect)? This way you can use the tag to find the UIimageView via the tag and move it where ever you want? Less expensive memory wise.

Jordan
Sorry -- it is adding it to an array... I just didn't put that in the code sample above. (that's why I put that comment there...I was trying to summarize my code, but I may have made it more confusing...)
Jeffrey Berthiaume