ddd
ddd

Reputation: 359

EXC_BAD_ACCESS at removeFromSuperview - using ARC

In one of my ViewControllers (ViewController A), I have the following code:

AlertViewController *aViewController = [[AlertViewController alloc] initWithNibName:@"AlertViewController" bundle:nil];
[self.view addSubview:[aViewController view]];
[self.view bringSubviewToFront:[aViewController view]];

And in AlertViewController, I have a button and when the user clicks on it, I have:

[self.view removeFromSuperview];

Whenever I click the button, the result is EXC_BAD_ACCESS. I'm unable to figure out the problem. My project is using ARC and ViewController A is part of a navigation controller stack if that info helps.

Upvotes: 0

Views: 1777

Answers (1)

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

The problem here is that the UIView doesn't own its UIViewController. In the first block of code you held the UIView around by adding it to a subview, but let the UIViewController go away. The UIView from a UIViewController is special, you can't let this happen.

Make sure the UIViewController that created the UIView lives as long as the view does.

Upvotes: 1

Related Questions