关闭 UIViewController 时 EXC_BAD_ACCESS 崩溃

2024-01-03

我有两个按钮触发到两个不同的 UIViewCOntroller,使用以下代码:

- (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;
    }
}

激活任一按钮都会转到适当的视图控制器,但会导致此警告:

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

我在每个视图控制器上都有一个保存和取消导航栏按钮。除了上面提到的之外,一切都按预期工作,除了视图控制器上的“取消”按钮newTransSegue,它会关闭 VC,但会导致应用程序崩溃并出现以下错误:

EXC_BAD_ACCESS (code = 1, address = 0x7f87394af29)

这是我用来关闭 VC 的委托方法:

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

我已经这样做了几天,并尝试删除转场并在故事板中重新创建它们,并对导航控制器执行相同的操作。我在某个地方出轨了,但看不出具体在哪里。

我肯定需要一些指导。 :)


好的,我研究了@Jay 提供的参考资料。原来我以前见过它,但只看过第 1 部分。在第 2 部分中,我发现了对“启用僵尸对象”的引用,我也这么做了。现在,当应用程序崩溃时,我收到了以下消息:[_UILayoutGuide isDescendantOfView:],它指出了 Storyboard 中的问题。

在检查 Storyboard 时,我发现令人惊讶的是,所讨论的视图控制器的表示形式与周围的视图控制器不同。我希望我能截取屏幕截图,但在最激烈的搜索过程中,我没有截取。

无论如何,进一步的研究出现了这个问题(及其相关评论) https://stackoverflow.com/questions/27681488/uilayoutguide-isdescendantofview-message-sent-to-deallocated-instance-0x174。我的调查显示,虽然我启用了尺寸类,但由于某种我无法解释的原因,模拟指标下相关视图控制器的尺寸已设置为“自由形式”。我将其重置为“推断”,现在一切似乎都正常运行 - 没有崩溃。希望我能详细解释一下,但我对结果很满意!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关闭 UIViewController 时 EXC_BAD_ACCESS 崩溃 的相关文章

随机推荐