添加了一个类别 (snippet2) 来跟踪保留计数和导航项的破坏,随意做同样的事情:) 似乎它没有被内存警告释放。一个解释来自于视图控制器不必与导航控制器一起使用的常识:这应该是为什么导航项添加了一个单独的类别(snippet1)并且它的生命周期必须用导航管理控制器,而不是视图控制器实例本身。
如果自定义导航项太重以至于您需要尽可能释放它,我将保留默认实现,添加自定义导航项类别并根据需要手动管理这些项目(再次通过覆盖所需的UINavigationController
方法,如导航-控制器didReceiveMemoryWarning
, pushViewController:animated:
, popViewControllerAnimated:animated:
)。但是,我无法想象真正需要这种情况的情况。
片段 1
@interface UIViewController (UINavigationControllerItem)
@property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.
@property(nonatomic) BOOL hidesBottomBarWhenPushed; // If YES, then when this view controller is pushed into a controller hierarchy with a bottom bar (like a tab bar), the bottom bar will slide out. Default is NO.
@property(nonatomic,readonly,retain) UINavigationController *navigationController; // If this view controller has been pushed onto a navigation controller, return it.
@end
片段 2
@implementation UINavigationItem (Logs)
- (id)init
{
NSLog(@"I'm initialized (%@)", [self description]);
self = [super init];
return self;
}
-(void) release
{
NSLog(@"I'm released [%d](%@)", [self retainCount], [self description]);
[super release];
}
-(void) dealloc
{
NSLog(@"I'm deallocated [%d](%@)", [self retainCount], [self description]);
[super dealloc];
}
@end