Jeffrey Berthiaume
Jeffrey Berthiaume

Reputation: 4594

Adding UIImageView to an NSMutableDictionary

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)

Upvotes: 0

Views: 1795

Answers (1)

Jordan
Jordan

Reputation: 21760

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.

Upvotes: 1

Related Questions