我以前遇到过这种问题,但没有得到满意的答案。
我通过了
NSMutableArray *counties
当我将第二个控制器推到导航堆栈上时。实际上,我设置了第二个控制器的“selectedCounties”属性(也是一个NSMutableArray),指针指向第一个控制器的“counties”,如下所示。
当我去
addObject
不过,我明白了:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
这是我的密码:
在SearchViewController.h中:
@interface SearchViewController : UIViewController
{
....
NSMutableArray *counties;
}
....
@property (nonatomic, retain) NSMutableArray *counties;
在SearchViewController.m中:
- (void)getLocationsView
{
[keywordField resignFirstResponder];
SearchLocationsViewController *locationsController =
[[SearchLocationsViewController alloc] initWithNibName:@"SearchLocationsView" bundle:nil];
[self.navigationController pushViewController:locationsController animated:YES];
[locationsController setSelectedCounties:self.counties];
[locationsController release];
}
在SearchLocationsViewController.h中:
@interface EventsSearchLocationsViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
...
NSMutableArray *selectedCounties;
}
...
@property (nonatomic, retain) NSMutableArray *selectedCounties;
在SearchLocationsViewController.m中(这里的重点是,我们正在切换表中的每个元素是否在所选县列表中处于活动状态):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([self.selectedCounties containsObject:[self.counties objectAtIndex:indexPath.row]]) {
//we're deselcting!
[self.selectedCounties removeObject:[self.counties objectAtIndex:indexPath.row]];
cell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"red_check_inactive.png"]];
}
else {
[self.selectedCounties addObject:[self.counties objectAtIndex:indexPath.row]];
cell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"red_check_active.png"]];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
[self.selectedCounties addObject....
在那里。
现在,当我自己登录时
[self.selectedCounties class]
这是怎么发生的?我理解类束(或者我想我无论如何也理解),但这是一个显式的特定类型,它在某个点上丢失了它的子类化,从而扼杀了整个过程。我完全不明白为什么会这样。