问题
i have a UIViewController
with UITableView
, when the tableView is empty i want to show another view so i am using this
[self.tableView setHidden:YES];
NoKidsViewController *noKids = [self.storyboard instantiateViewControllerWithIdentifier:@"NoKidsView"];
[self.view addSubview:noKids.view];
all is fine, i'm able to see the view. but when i tap on one of the buttons in it i'm getting the EXC_BAD_ACCESS EXC_I386_GPFLT
error.
//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 over 3 hours trying to find any solution w/o success. what could cause that error? and how can i fix it?
回答1:
The noKids
controller is going out of scope and being deallocated. This is what is often referred to as a zombie object.
You need to add the noKids
controller to the childViewControllers
of the containing controller.
NoKidsViewController *noKids = [self.storyboard instantiateViewControllerWithIdentifier:@"NoKidsView"];
[self addChildViewController:noKids];
[self.view addSubview:noKids.view];
[noKids didMoveToParentViewController:self];
This will retain the NoKidsViewController
as well as allow the view controller methods to pass down to it. For more information on creating your custom container view controller:
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
来源:https://stackoverflow.com/questions/25268744/exc-bad-access-exc-i386-gpflt-while-click-on-button