EXC_BAD_ACCESS crash upon dismissing UIViewControl

2019-07-20 01:35发布

问题:

I have two buttons triggering segues to two different UIViewCOntrollers, using this code:

- (IBAction)newTransButton:(UIButton *)sender
{
    [self performSegueWithIdentifier:@"newTransSegue" sender:self];
}

- (IBAction)switchAccountButton:(UIButton *)sender
{
    [self performSegueWithIdentifier:@"selectAccountSegue" sender:self];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    if ([[segue identifier] isEqualToString:@"newTransSegue"])
    {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AddTransactionVC *atvc = (AddTransactionVC *)navController.topViewController;
        atvc.delegate = self;

        WMMGTransaction *addedTransaction = (WMMGTransaction *)[WMMGTransaction MR_createInContext:localContext];

        addedTransaction.account = self.currentAccount.name;
        atvc.thisTransaction = addedTransaction;
    }

    else if ([[segue identifier] isEqualToString:@"selectAccountSegue"])
    {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AccountSelectVC *acctSelVC = (AccountSelectVC *)navController.topViewController;
        acctSelVC.delegate = self;
    }
}

Activation of either button segues to the appropriate view controller, but causes this warning:

Warning: Attempt to present <UINavigationController: 0x7fb99b4dd430> on <FirstViewController: 0x7fb99b565dd0> whose view is not in the window hierarchy!

I have a Save and a Cancel Navigation bar button on each View controller. Other than as mentioned above, everything works as expected, except for the Cancel button on the View controller at newTransSegue, which dismisses the VC, but crashes the app with this error:

EXC_BAD_ACCESS (code = 1, address = 0x7f87394af29)

Here is the delegate method I use to dismiss that VC:

-(void)addTransactionViewControllerDidCancel
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

I've been at this for a couple of days, and have tried deleting the segues and recreating them in storyboard, as well as doing the same for the navigation controllers. I've gone off the rails somewhere, but can't see exactly where.

I could sure use some guidance. :)

回答1:

OK, I studied the reference kindly provided by @Jay. Turns out I had seen it before, but only Part 1. In Part 2, I discovered a reference to Enable Zombie Objects, which I did. Now, when the app crashed, I was provided with this message: [_UILayoutGuide isDescendantOfView:], which pointed to an issue in the Storyboard.

Upon examining the Storyboard, I discovered that, surprisingly, the representation of the view controller in question was shaped differently than the surrounding view controllers. I wish I'd made a screenshot, but in the heat of the hunt, I didn't.

In any case, further research turned up this question (and its associated comments). My investigation revealed that, while I had Size Classes enabled, for some reason I can't explain, the size of the relevant View Controller under Simulated Metrics had been set to "Freeform." I reset it to "Inferred" and things appear to be operating normally now--no crashes. Wish I could explain it in detail, but I'm happy with the result!