EXC_BAD_ACCESS EXC_I386_GPFLT on button click
I have UIViewController
with UITableView
, when the tableView is empty, I want to show a different view, so I use this
[self.tableView setHidden:YES];
NoKidsViewController *noKids = [self.storyboard instantiateViewControllerWithIdentifier:@"NoKidsView"];
[self.view addSubview:noKids.view];
all right, i can see the performance. but when i click one of the buttons in it i get an error EXC_BAD_ACCESS EXC_I386_GPFLT
.
//NoKidsViewController
- (IBAction)addNewKid:(id)sender {
AddKid *addKidController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddKid"];
[self.navigationController pushViewController:addKidController animated:YES];
}
- (IBAction)saleSpot:(id)sender {
SaleSpot *saleSpotController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddKid"];
[self.navigationController pushViewController:saleSpotController animated:YES];
}
I searched the net after 3 hours trying to find any solution with no success. what can cause this error? and how can i fix this?
source to share
The controller noKids
is out of scope and released. This is what is often referred to as a zombie object.
You need to add a controller noKids
to the childViewControllers
containing controller.
NoKidsViewController *noKids = [self.storyboard instantiateViewControllerWithIdentifier:@"NoKidsView"];
[self addChildViewController:noKids];
[self.view addSubview:noKids.view];
[noKids didMoveToParentViewController:self];
This will save NoKidsViewController
and also allow the view controller methods to navigate to it. For more information on creating a custom container view controller:
source to share