objective-c - removeFromSuperview 处的 EXC_BAD_ACCESS - 使用 ARC

标签 objective-c ios uiviewcontroller

在我的一个 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/

相关文章:

ios - 是否存在与 Swift 的 fatalError 等效的 Objective-C?

ios - 向 UILabel 添加两个阴影

iphone - 如何将 CornerRadius 赋予 UIViewController 对象 iPhone

ios - 在索引处添加 NSMutableArray 的元素与其他 NSMutableArrays 的相应元素

ios - ABPeoplePickerNavigationController 和 UIImagePickerController 在 iPad iOS7 上无法正确显示

ios - 将连接间隔从默认的 30 毫秒缩短

ios - 动画按钮。重复使@IBAction 不起作用

ios - 以编程方式在 UIButton 上设置目标/操作?

swift - 如何在 Swift 中向下转换 UIViewController

ios - 为什么 UISupportedInterfaceOrientations 设置不影响 UIViewController 行为?