ios - [CustomViewController respondsToSelector :]: message sent to deallocated instance

标签 ios memory-management automatic-ref-counting uisplitviewcontroller

这用于我的 ARC 前代码工作正常,但自从重构所有项目以与 ARC 兼容后,我开始遇到此崩溃:

[CustomViewController respondsToSelector:]: message sent to deallocated instance

我的项目是一个带有 Split View的 iPad 应用程序,但与苹果文档相反,之前的开发人员在 Split View之前将另一个 View Controller 放在应用程序启动时可见。所以我知道这不是正确的做法,但正如我所说,它曾经在 ARC 集成之前工作,所以我需要解决这个问题。

包含项目菜单的 Root View Controller ,每个项目显示一个要填写的详细信息表单,然后单击下一步按钮移动到下一个详细信息屏幕等。

当我点击放在 Root View 上的主页按钮返回主视图时,问题就开始了,这里是将用户移动到主屏幕的相关代码:

//this method is in the appdelegate, and it gets called when clikc on home button located on the root view
- (void) showHome
{
    homeController.delegate = self;

    self.window.rootViewController = homeController;
    [self.window makeKeyAndVisible];
}

然后,当我单击按钮返回 Split View( Root View /详细信息 View 在哪里)时,应用程序崩溃并显示上述描述。我使用仪器分析了应用程序,负责的代码行位于 RootViewController 中,在 didSelectRowAtIndexPath 方法中,这里是相关代码:

    if(indexPath.row == MenuCustomViewController){
            self.customVC=[[CustomViewController alloc] initWithNibName:@"CustomVC"
                                                                      bundle:nil];
            [viewControllerArray addObject:self.customVC];
            self.appDelegate.splitViewController.delegate = self.customVC;
}

customVC 是一个强大的属性,我尝试直接分配并分配给实例变量,但这无助于修复崩溃。有什么想法吗?

编辑: 这是仪器给出的堆栈跟踪:

[self.appDelegate.splitViewController setViewControllers:viewControllerArray];//this line caused the crash

[viewControllerArray addObject:self.appDescVC];//this statement is called before the above one

self.custinfoVC=[[CustInfoViewController alloc] initWithNibName:@"CustInfo" bundle:nil];//this statement is called before the above one

self.appDelegate.splitViewController.delegate = self.appDescVC;//this statement is called before the above one

custinfoVCappDescVC 是强大的属性。

最佳答案

我通过在 dealloc 方法中将我的委托(delegate)和数据源设置为 nil 解决了这个问题。不确定它是否对您有帮助,但值得一试。

- (void)dealloc
{
    homeController.delegate = nil;

    //if you have any table views these would also need to be set to nil
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

关于ios - [CustomViewController respondsToSelector :]: message sent to deallocated instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18258013/

相关文章:

c - 无法从函数中释放内存

iOS:垃圾收集

ios - 修补 Manifest/Plist 以使用不同的平台特定设置进行构建

ios - 无法解析调试映射

android - SQLite 在 Android 和 iOS 上的性能差异

javascript - 无法在 Safari 上的 IndexedDB 中存储 Blob 类型

python - numpy.ndarray 对象未被垃圾收集

c - 当 C 中的静态分配没有足够的内存时会发生什么?

ios - Table Cell 子类从未发布?

iPhone:在循环中创建 NSString 时出现巨大的内存峰值