【问题标题】:iOS memory warning case crash with error "message sent to deallocated instance"iOS 内存警告案例崩溃并出现错误“已发送到已释放实例的消息”
【发布时间】:2014-07-21 10:47:54
【问题描述】:

将我的视图控制器两次推送到导航控制器并模拟内存警告,导致应用程序崩溃并出现错误:“message sent to deallocated instance”

我通过按下按钮来推动视图控制器:

-(void)buttonPressed
{
    MyViewCOntroller *vc = [[MyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
  • 我正在使用 ARC。

场景:

  1. [self buttonPressed];
  2. 在 vc 中按返回按钮。
  3. [self buttonPressed];
  4. 在 vc 中按返回键
  5. 模拟内存警告

如果只推送一次,不会发生崩溃。

我也尝试将“vc”移动为父控制器的ivar,但效果是一样的......

也许这会有所帮助,但我使用的是自定义后退按钮及其选择器:

-(void)backButtonPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

【问题讨论】:

  • 启用僵尸并调试它
  • 已经这样做了。它指向“buttonPressed”方法。我就是这样找到这个地方的。但我无法理解原因以及如何处理。
  • 尝试使用 segue 推送
  • 您是否尝试从 MyViewCONtroller 访问父 vc?
  • 在 viewDid 出现之前推送任何视图控制器也会导致在 iOS7 中崩溃,如果您在第二次推送视图时稍等片刻,您能告诉我同样的问题吗?

标签: ios objective-c uinavigationcontroller


【解决方案1】:

创建这个 vc 对象作为类成员并检查是否存在崩溃

 @interface ParentViewController()
 {
    MyViewCOntroller *vc;
 }
 @end

 @implementation PaintingViewController


  -(void)buttonPressed
  {
     vc = [[MyViewController alloc] init];
     [self.navigationController pushViewController:vc animated:YES];
  }

【讨论】:

  • 你的意思是让它静态化?
  • 尝试移动 vc = [[MyViewController alloc] init];查看DidLoad
【解决方案2】:

找到了解决方案。请参阅我对其他问题的回答:

[UINavigationController retain]: message sent to deallocated instance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-31
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多