EXC_BAD_ACCESS EXC_I386_GPFLT while click on button

大兔子大兔子 提交于 2019-12-19 09:50:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!