单击按钮时EXC_BAD_ACCESS EXC_I386_GPFLT [英] EXC_BAD_ACCESS EXC_I386_GPFLT while click on button
问题描述
我有一个UIViewController
和UITableView
,当tableView为空时,我想显示另一个视图,所以我正在使用它
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];
一切都很好,我能够看到风景.但是当我点击其中的一个按钮时,出现了EXC_BAD_ACCESS EXC_I386_GPFLT
错误.
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];
}
我在3个小时内搜寻了整个网络,试图找到没有成功的解决方案.什么会导致该错误?以及我该如何解决?
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?
推荐答案
noKids
控制器超出范围并被释放.这就是通常被称为僵尸对象的东西.
The noKids
controller is going out of scope and being deallocated. This is what is often referred to as a zombie object.
您需要将noKids
控制器添加到包含控制器的childViewControllers
.
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];
这将保留NoKidsViewController
并允许视图控制器方法向下传递给它.有关创建自定义容器视图控制器的更多信息:
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:
这篇关于单击按钮时EXC_BAD_ACCESS EXC_I386_GPFLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!