简体   繁体   English

关闭View Controller时的Exec_bad_access

[英]Exec_bad_access when dismissing View Controller

for some time I'm struggling to find the solution to this, I've searched google, stackoverflow but couldn't solve this. 一段时间以来,我一直在努力寻找解决方案,我已经搜索过Google,stackoverflow,但无法解决此问题。

I have 2 view controllers (let's call them VC1 and VC2), I can change between them with no problem. 我有2个视图控制器(我们称它们为VC1和VC2),我可以在它们之间进行切换而不会出现问题。 To present VC2 I use: 要呈现VC2,我使用:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UINavigationController* ctrl = [storyboard instantiateViewControllerWithIdentifier:@"histListID"];
[self presentViewController:ctrl animated:YES completion:nil];

To get back to VC1 I use: 回到VC1,我使用:

- (IBAction)backPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

VC2 contains a Table View and after I delete some cell if I try to get back to VC1 I get EXC_BAD_ACCESS (code = 1). VC2包含一个表视图,如果我尝试返回VC1,则删除一些单元格后,我得到EXC_BAD_ACCESS(代码= 1)。 I've never had to handle this before and I know it may have to do with something somewhere in my code. 我以前从未处理过这个问题,我知道这可能与代码中的某些地方有关。 But I don't really know how to interpret it. 但是我真的不知道如何解释它。 Does it have something to do with my tableView? 它与我的tableView有关系吗? Am I doing something wrong when dismissing the controller? 解雇控制器时我做错什么了吗?

线程1

If you need more code please ask, I really need to solve it. 如果您需要更多代码,请询问,我真的需要解决它。

PS: I have ARC turned on. PS:我已打开ARC。

EDIT: Code used to delete cell 编辑:用于删除单元格的代码

    [self deleteItemAt:chosenItem];
    [tableView reloadData];

Basically what deleteItemAt does is delete some objects from arrays: 基本上deleteItemAt所做的是从数组中删除一些对象:

- (void)deleteItemAt:(int)index
{

    [self.histModel deleteFileAtIndex:index];
    if ([self isWeekSelected]) {
        [self updateWeekDic];
    }else{
        [self updateMonthDic];
    }
    [callout dismissAnimated:YES];
}

Sorry if it doesn't do it correctly I didn't wrote that part of the code, but I don't think it has to do with deleting the cell. 抱歉,如果操作不正确,我没有写代码的那部分,但是我认为这与删除单元格无关。

Whenever you dismiss a viewController make sure to set all delegates to nil. 每当您关闭viewController时,请确保将所有委托都设置为nil。 The stack trace is showing a lot of objects being released and then tableview delegate callbacks. 堆栈跟踪显示了许多对象被释放,然后显示了tableview委托回调。 I suspect from this that its trying to call [delegate canEditCellAtRowindex] but the viewController has been dismissed and is no longer in the stack. 我由此怀疑,它试图调用[delegate canEditCellAtRowindex]但是viewController已被关闭并且不再在堆栈中。

eg 例如

[tableView setDelegate:nil];
[tableView setDataSource:nil];

In my opinion approved answer is not very good, because as it said in comments, you still have warning in console. 我认为批准的答案不是很好,因为正如评论中所说,您在控制台中仍然有警告。 I would prefer call [self.tableView setEditing:NO animated:NO] in view controller's dealloc or in viewDidDisappear: . 我希望在视图控制器的deallocviewDidDisappear:中调用[self.tableView setEditing:NO animation:NO]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM