Adding UIImageView to NSMutableDictionary
I am trying to add multiple instances of UIImageView (s) to NSMutableArray from NSMutableDictionar (s).
I do this to be able to go back and pull the UIImageView link to reposition those images.
It looks like when I dump information from the Dictionary, it only returns a null pointer ...
Any suggestions on how I should implement this? Maybe a way is easier to store a list of n images and be able to refer to 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 am using a dictionary because I also store the dy / dx value for the rate of change of motion)
a source to share
The code above doesn't work for me.
You are creating a new NSMutableDictionary * dictBall every time through the loop. Is there something holding it back after the cycle exists? It doesn't look like your code. This is probably why your object is null.
Move that line outside / in front of the for loop and you should be fine.
Also are you sure you want to store the UIViewImage in the dictionary? Why not just ID (based on tag) and coordinates (CGPoint or CGRect)? So you can use the tag to find the UIimageView through the tag and move it wherever you want? Less expensive memory.
a source to share