ViewControllerA
使用模态segue打开ViewControllerB
。
ViewControllerA
:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// ModalSegue is defined in the storyboard to point to ViewControllerB
[self performSegueWithIdentifier:@"ModalSegue" sender:self];
}
ViewControllerB
:
- (IBAction)cancelButtonTapped:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil]; // Causes crash
}
在iOS 7.1中,这会导致EXC_BAD_ACCESS崩溃。如果启用了Zombie Objects,则抛出异常:
*** -[ViewControllerB respondsToSelector:]: message sent to deallocated instance 0x12ed7e170
在iOS 7.0中,这可以按预期工作。
有什么想法吗?
编辑:根据LeoNatan的请求,这里是dealloc
中ViewControllerB
方法的堆栈跟踪:
答案 0 :(得分:6)
正如聊天中所讨论的,问题是选择器视图的寿命比视图控制器长,导致它尝试向其代理发送消息。
解决方案是在nil
方法中将选择器视图的委托和数据源设置为dealloc
。
对于iOS 7及更高版本,将代理和数据源设置为nil
被认为是一种好习惯,因为视图具有比视图控制器更长的生命周期,并且在这些视图被释放后尝试访问它们的代理。 / p>