I have a view that displays in a UIPopoverController. Before, it just had properties that were associated with it’s view for a single object. So it would be something like
TargetDetailView : NSObject
@property Target *target
- (id)initWithTarget:(Target *)target
Now however, in some situations depending on what level zoom the user is looking at our view, there will be multiple targets in the popover. So I added an NSMutableArray *targets property to the class in order to show multiple targets in the popover.
Right now, the actual target objects that get drawn on our view are getting drawn on top of each other which is a waste.
The initWithTarget method only got called when you actually clicked on a target and the popover got presented.
What I’m trying to do now is calculate ahead of time where I am goign to have multiple targets in the same location, draw it only once, but when pressed have the popover show and this time, use the NSArray *targets to search through to show the multiple targets in that location.
The problem I have is I do not know where to initialize the memory for the NSMutableArray *targets. The TargetDetailView only gets initialized when you press the target, but I want to calculate ahead of time from my other class when the target views are on top of each other to only draw it once.
I sort my targets in order in terms of location, then I look to see if the first two are similar enough in location to bin them:
for (int i = 0; i < [sortedArray count] - 1; i++) {
TargetDetailView *firstTargetDetailView = (TargetDetailView *)[sortedArray objectAtIndex:i];
TargetDetailView *secondTargetDetailView = (TargetDetailView *)[sortedArray objectAtIndex:i + 1];
if (secondTargetDetailView.target.location - firstTargetDetailView.target.location < scale) {
[firstTargetDetailView.targets addObject:secondTargetDetailView.target]; // crash
[newTargetDetailViewArray addObject:(TargetDetailView *)[sortedArray objectAtIndex:i]];
}
}
I try to have the first TargetDetailView.target property point to the next TargetDetailView if it’s close enough in location. However, the .targets property never gets initialized since my TargetDetailView only gets initialized on press.
How should I either
a) allocate memory for this object
b) refactor and/or allocate memory for this object
Thanks.
If I may make a suggestion, you could use the same initWithTarget method but always keep your targets in an array. You could then initialize your array there and add your Target parameter from the start, like this:
You can then use the targets property to add more targets (if you’d like) and use a simple loop through targets without having to care about the number of items in there.