【问题标题】:EXC_BAD_ACCESS when remove from superview从超级视图中删除时的 EXC_BAD_ACCESS
【发布时间】:2011-11-23 10:14:30
【问题描述】:

我的项目使用 ARC,所以我不能使用保留或释放,在 ViewController A 中,我初始化 ViewController B 并将其视图添加为子视图:

ViewControllerB *viewB = [[ViewControllerB alloc] init];

[self.view addSubview:viewB.view];

在 ViewControllerB 中,我创建了一个按钮,当用户单击它时,视图将从 superview 中删除:

[self.view removeFromSuperview];

结果是 EXC_BAD_ACCESS。 请帮助我,对我的英语感到抱歉。

【问题讨论】:

    标签: uiviewcontroller exc-bad-access addsubview superview


    【解决方案1】:

    我认为 viewB 不是局部变量。

    【讨论】:

      【解决方案2】:

      我也有同样的问题..

      试试这样的:

      NSLog("self.view retain count: %d", self.view.retainCount);
      [self.view removeFromSuperview];
      NSLog("self.view retain count: %d", self.view.retainCount);
      

      或:

      NSLog("self.view.superview retain count: %d", self.view.superview.retainCount);
      [self.view removeFromSuperview];
      NSLog("self.view.superview retain count: %d", self.view.superview.retainCount);
      

      这不是一个好习惯,但也许你会发现内存泄漏错误...... 如果您使用的是 ARC - 它并不能保护您免受内存管理错误的影响……

      如果我的英语有问题,我很抱歉。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多