【问题标题】:How to handle release-es when using lots of sub-viewcontrollers?使用大量子视图控制器时如何处理 release-es?
【发布时间】:2012-03-21 21:35:41
【问题描述】:

我在这里有点修复。应用程序搜索 Core Data 并将其呈现在屏幕上,使用 50 多个新的 UIViewController 子类添加到当前视图。当新的搜索出现时,这些 VC 将从视图中删除并应该被释放。但是,我不保留对 VC 的引用。

我尝试使用保留它们的数组,但这似乎不起作用。遇到这种事怎么办?

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++;
}

非常感谢任何帮助!

【问题讨论】:

    标签: objective-c memory-management uiviewcontroller


    【解决方案1】:

    我在做的一个项目中遇到了类似的问题,我最终使用了 UITableView,每次我需要创建一个新的 UITableViewCell 时,我都会创建它的 UIViewController,将视图控制器添加到数组并设置 [ UITableViewCell tag] 到数组中视图控制器的索引。如果 [UITableView dequeTableViewCell...] 返回一个我可以使用的 UITableViewCell,我将通过使用 [UITableViewCell 标签] 作为其视图控制器的索引来获取它的 UIViewController,我的视图控制器有一个方法,因此我可以更改它所代表的数据对象。因为我一次只有一个 UITableViewCell 可见,(在它们之间滚动时两个)我只需要两个 UIViewController。我还使用了一个技巧来让 UITableView 横向滚动而不是上下滚动。

    【讨论】:

      【解决方案2】:

      当您调用 removeAllObjects 时,它会将数组中每个对象的 refCount 减少到 -1,但它仍然是正数。

      您有很多泄漏,因为您没有在每次创建 gridVC 时发布它:

      GridViewCellController *gridVC = [[GridViewCellController alloc] initWithNibName:@"GridViewCellController" bundle:nil];
      ...
      [gridVCs addObject:gridVC];
      [gridVC release];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多