在我的一个 ViewController(ViewController A)中,我有以下代码:
AlertViewController *aViewController = [[AlertViewController alloc] initWithNibName:@"AlertViewController" bundle:nil];
[self.view addSubview:[aViewController view]];
[self.view bringSubviewToFront:[aViewController view]];
在 AlertViewController 中,我有一个按钮,当用户单击它时,我有:
[self.view removeFromSuperview];
每当我单击该按钮时,结果都是 EXC_BAD_ACCESS。 我无法找出问题所在。我的项目正在使用 ARC,如果该信息有帮助的话,ViewController A 是导航 Controller 堆栈的一部分。
最佳答案
这里的问题是 UIView 不拥有它的 UIViewController。在第一个代码块中,您通过将 UIView 添加到 subview 来保留 UIView,但让 UIViewController 消失。 UIViewController 中的 UIView 很特殊,你不能让这种情况发生。
确保创建 UIView 的 UIViewController 与 View 一样长。
关于objective-c - removeFromSuperview 处的 EXC_BAD_ACCESS - 使用 ARC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730565/