UIViewController从表视图中显示 - 弹出时不被破坏

时间:2021-07-22 15:50:28

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

我有一个表视图,只要轻敲一行就显示UIViewController(显示该特定行的详细信息)。

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 :

我在“详细”UIViewController类中有许多IBOutlets和Variable,我在dealloc中发布如下:

- (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.

好的,上面的代码现在完美无缺。关键似乎是将细节设置为零 - 虽然不确定原因。

3 个解决方案

#1


1  

Couple things:

情侣:

  1. You are alloc/init'ing it but not releasing it. That's a problem.
  2. 你是分配/初始化它但不释放它。那是个问题。
  3. You are setting it to a property (which is probably set to retain). That's a problem because of (1).
  4. 您将其设置为属性(可能设置为保留)。这是因为(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.

因此,要解决此问题,请在完成本地范围(该方法结束)之后释放对象。因为它是一个属性,所以在拥有类的dealloc方法中释放它。

#2


1  

alloc/init - retain count: 1

alloc / init - 保留计数:1

setting with property - retain count: 2

设置属性 - 保留计数:2

pushing onto nav. controller - retain count: 3

推进导航。控制器 - 保留计数:3

popping off nav. controller - retain count: 2

弹出导航。控制器 - 保留计数:2

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

这假设属性具有保留标志,但无论如何,保留计数永远不会变为0

#3


1  

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

假设细节是保留属性。覆盖getter以进行延迟实例化。

- (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.

另外请确保您仅在-dealloc中发送-release详细信息。你的-tableView:didSelectRowAtIndexPath:现在看起来像这样。

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

#1


1  

Couple things:

情侣:

  1. You are alloc/init'ing it but not releasing it. That's a problem.
  2. 你是分配/初始化它但不释放它。那是个问题。
  3. You are setting it to a property (which is probably set to retain). That's a problem because of (1).
  4. 您将其设置为属性(可能设置为保留)。这是因为(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.

因此,要解决此问题,请在完成本地范围(该方法结束)之后释放对象。因为它是一个属性,所以在拥有类的dealloc方法中释放它。

#2


1  

alloc/init - retain count: 1

alloc / init - 保留计数:1

setting with property - retain count: 2

设置属性 - 保留计数:2

pushing onto nav. controller - retain count: 3

推进导航。控制器 - 保留计数:3

popping off nav. controller - retain count: 2

弹出导航。控制器 - 保留计数:2

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

这假设属性具有保留标志,但无论如何,保留计数永远不会变为0

#3


1  

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

假设细节是保留属性。覆盖getter以进行延迟实例化。

- (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.

另外请确保您仅在-dealloc中发送-release详细信息。你的-tableView:didSelectRowAtIndexPath:现在看起来像这样。

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