I’m in a bit of a fix here. An app searches Core Data and presents that on screen, using 50+ new UIViewController subclasses that get added to the current view. When a new search comes, these VC’s are removed from the view and should be released. However, I don’t keep references to the VC’s.
I tried with an array that keeps them, but that doesn’t seem to work. How do I handle such a thing?
for (UIView *view in scrollView.subviews) {
[view removeFromSuperview];
}
[gridVCs removeAllObjects];
int numItems = [searchResults count];
int n = 0;
for(Plant *plant in searchResults)
{
GridViewCellController *gridVC = [[GridViewCellController alloc] initWithNibName:@"GridViewCellController" bundle:nil]; // !!! XXX
[gridVC setPlant:plant];
[gridVC setSearchVC:self];
gridVC.label.text = plant.naamnl;
[gridVC.imageView setImage:[UIImage imageWithContentsOfFile:foto_url]];
[scrollView addSubview:gridVC.view];
[gridVCs addObject:gridVC];
n++;
}
Any help is greatly appreciated!
I had a similar issue with a project I was working on, I ended up using a UITableView, every time I need to create a new UITableViewCell I would instead create its UIViewController, add the view controller to an array and set the [UITableViewCell tag] to the index of the view controller in the array. If [UITableView dequeTableViewCell…] returned a UITableViewCell I could use I would get its UIViewController by using the [UITableViewCell tag] as the index of its view controller, my view controller had a method so I could change the data object it represented. Since I only had one UITableViewCell visible at a time, (two when scrolling between them) I only ever needed two UIViewController. I also used a trick to get UITableView to scroll sideway instead of up and down.