0

当我单击按钮时,UIAlertView 会提示用户输入名称。然后这个名称被创建为一个新的“客户”对象并插入到一个可变数组中。

有一个名为“CustListByName”的单独可变数组,它存储所有名称的列表。

我遇到的问题是,当添加第二个或第三个名字时,应用程序崩溃了。有时会在第二次尝试时发生,有时在第三次尝试时发生。除了 (lldb) 之外,调试器中没有给出任何信息。该程序报告 EXC_BAD_ACCESS,然后将我转储到带有一堆汇编代码的屏幕。

崩溃发生在这些代码行中:

本质上,它会清除名称数组,然后根据对象数组重新填充它。我已经用断点一步一步地研究过,但直到崩溃点之前一切似乎都是正确的。这也令人困惑,为什么在第二次或第三次尝试时会发生这种情况,而不是第一次。

[custListByName removeAllObjects];
    for (Customer *object in custListByObject) {
        [custListByName addObject:object->name];
    }

这是每次单击新客户按钮时创建和插入客户的代码:

   Customer *tempCust = [[Customer alloc] init];
    tempCust->name =[[alertView textFieldAtIndex:0] text];
    [custListByObject addObject:tempCust];
    [tempCust release];

我真的很感激这方面的帮助,谢谢!

4

1 回答 1

0

What I suspect is happening is that the UIPickerView is attempting to load a row using information from your customer array after you have already cleared it, and before you repopulate it. This would cause a bad access error.

What you may consider doing instead, is keeping two arrays, an NSMutableArray for loading the customers, and an NSArray as the actual data source for the UIPickerView. Then right before you reload the UIPickerView, you say:

dataSourceArray = [loadingArray copy];
[pickView reloadAllComponents];

Hopefully this helps.

Edit:

Here's what your updated code would look like if your loading array was called loadingCustListByName:

[loadingCustListByName removeAllObjects];
    for (Customer *object in custListByObject) {
        [loadingCustListByName addObject:object->name];
    }
custListByName = [loadingCustListByName copy];
[pickView reloadAllComponents];

Doing this will ensure that the UIPickerView's datasource array always matches up with the number of rows it thinks it has.

于 2013-04-29T22:43:41.497 回答