UIViewController没有被释放

时间:2011-11-03 16:14:02

标签: iphone objective-c release viewcontroller dealloc

我正在尝试将自定义UIViewController类弹出到导航控制器,然后当不再查看该弹出控制器时,将其取消分配。但是,视图控制器仍然存在,我不知道为什么。任何帮助将不胜感激。

接口:

@property (nonatomic, retain) CustomViewController *viewcontroller;

实现:

CustomViewController *vc = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil];

self.CustomViewController = vc;

[vc release]; // Release this after setting property to avoid memory leak.

[[self navigationController] pushViewController:[self.CustomViewController autorelease] animated:YES];

// Push in the next view. My thinking is that since pushViewController retains this view, if I autorelease when I send the view, the only thing still retaining the view when it shows up will be the navigation controller, and thus when it is bounced back off of the navigation stack, it should be deallocated. But apparently that's not happening..

1 个答案:

答案 0 :(得分:1)

你的alloc和init给它一个保留计数为1,通过CustomViewController属性再次给它一个保留消息,把它带到2。

您需要执行以下操作,以便释放......

self.CustomViewController = nil;

虽然我不确定你为什么要存储对它的引用??!