在我的根视图中,我有一个包含三行的tableview。当我单击任一行时,它将显示一个新视图,然后我可以按下由我的导航控制器自动创建的后退按钮。当我尝试单击之前已经选择的行时,我的问题出现了。我收到一条EXC BAD ACCESS错误消息。我认为这是必要的所有代码:
- (void)dealloc
{
self.rowChosenArray = nil;
self.rootChoicesArray = nil;
self.customImage = nil;
self.rootTableView = nil;
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.rootChoicesArray = [[[NSMutableArray alloc] initWithObjects:@"",@"See Today's Deals!", @"My Purchased Deals", @"Personal Settings", nil] autorelease];
self.rowChosenArray = [[[NSMutableArray alloc] initWithObjects:@"", nil] autorelease];
DealsViewController *dealsViewController = [[DealsViewController alloc] initWithNibName:@"DealsViewController" bundle:nil];
[self.rowChosenArray addObject:dealsViewController];
[dealsViewController release];
PurchasedDealsViewController *purchasedDealsViewController = [[PurchasedDealsViewController alloc] initWithNibName:@"PurchasedDealsViewController" bundle:nil];
[self.rowChosenArray addObject:purchasedDealsViewController];
[purchasedDealsViewController release];
SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
[self.rowChosenArray addObject:settingsViewController];
[settingsViewController release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *targetViewController = [rowChosenArray objectAtIndex: indexPath.row];
[[self navigationController] pushViewController:targetViewController animated:YES];
[targetViewController release];
}
我仍然不熟悉何时/何时将对象释放/设置为nil(我的所有四个对象在头文件中都有retain属性。
提前感谢您的帮助,如果您还有其他需要,请告诉我。
答案 0 :(得分:1)
答案 1 :(得分:1)
您在targetViewController
中过度发布tableView:didSelectRowAtIndexPath:
。您通过objectAtIndex:
获得了非拥有引用(这意味着您不对该引用负责)并且可能不会发布该引用。所以只需删除[targetViewController release];
,一切都很好。
规则是这样的:如果您retain
编辑或通过名称为
alloc
,copy
或mutableCopy
new
答案 2 :(得分:0)
此处也有一些泄密,如果你留在viewDidLoad
,你应该在viewDidUnload
和dealloc