开发者

UIViewController Presented from Table View - Not being destroyed when popped

I have a table view which presents a UIViewController whenever a row is tapped (displays details for that particular row).

The code is as follows :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath       *)indexPath 

{ 

if (!detail) {

detail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];        

}

PlaceObject *info = [locationInfo objectAtIndex:indexPath.row];

detail.UniqueID = info.UniqueID;
detail.hidesBottomBarWhenPushed = YES;

[self.navigationController pushViewController:detail animated:YES];

self.detail = nil;
[detail release];

}

The problem is that "detail" does not seems to be destroyed when it is popped from the stack (when the user goes back开发者_高级运维 to the table view).

I have a number of IBOutlets and Variable in the "detail" UIViewController class which I release in dealloc as follows :

- (void)dealloc
{
NSLog(@"Deallocing");

[storedURL release];
[storedNumber release];
[storedLocation release];
[nameLabel release];
[postCode release];
[description release];
[openTime release];
[nearestTube release];
[area release];
[image release];
[name release];
[phoneNumber release];
[scroll release];
[picture release];
[addressOne release];
[cost release]; 

[super dealloc];

}

Can anybody advise why "details" may not be being destroyed when the user returns to the table view ?

EDIT

Ok the above code now works perfectly. The key seemed to be setting detail to nil - not sure why though.


Couple things:

  1. You are alloc/init'ing it but not releasing it. That's a problem.
  2. You are setting it to a property (which is probably set to retain). That's a problem because of (1).

So, to fix this, release the object after you are done with in in the local scope (end of that method). And since it is a property, release it in the dealloc method of the owning class.


alloc/init - retain count: 1

setting with property - retain count: 2

pushing onto nav. controller - retain count: 3

popping off nav. controller - retain count: 2

This is making the assumption that the property has the retain flag but either way, your retain count is never getting to 0


Assume that detail is a retained property. Override the getter to do lazy instantiation.

- (DetailViewController *)detail
{
    if (!detail)
    {
        detail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        detail.hidesBottomBarWhenPushed = YES;
    }

    return detail;
}

Additionally be sure that you're sending -release to detail in -dealloc only. Your -tableView:didSelectRowAtIndexPath: now looks like this.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath       *)indexPath 
{ 
    detail.UniqueID = [locationInfo objectAtIndex:indexPath.row].UniqueID;
    [self.navigationController pushViewController:self.detail animated:YES];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜