使用navBar进行内存泄漏

时间:2011-05-14 21:38:07

标签: iphone

你可以帮我解决内存泄漏吗?

#import <Foundation/Foundation.h>


@interface NavBar : NSObject 
{
    NSString* nav;
}

@property (nonatomic, retain) NSString* nav;

+ (NavBar *) sharedInstance;

@end


#import "NavBar.h"


@implementation NavBar

@synthesize nav;

-(void)dealloc
{
    [nav release];
}

+(NavBar *)sharedInstance 
{
    static NavBar *myInstance = nil;
    if (nil == myInstance) {
        myInstance  = [[[self class] alloc] init];
    }
    return myInstance;
}

@end
我在这里漏了一把:

- (void) viewWillAppear:(BOOL)animated 
{
    [NavBar sharedInstance].nav = @"navBar.png";
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:19/255.0 green:140/255.0 blue:130/255.0 alpha:1.0];

    [super viewWillAppear:animated];
}

在我的应用的代表中:

@interface UINavigationBar (CustomBackground)
- (void)drawRectCustomBackground:(CGRect)rect;
@end


@implementation UINavigationBar (CustomBackground)

- (void)drawRectCustomBackground:(CGRect)rect 
{

    if (self.barStyle == UIBarStyleDefault)
    {

            UIImage *image = [UIImage imageNamed:[NavBarStyles sharedInstance].navStyle];
            [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
            return;
    }

    [self drawRectCustomBackground:rect];
}

1 个答案:

答案 0 :(得分:1)

在某些时候,你需要打电话

 [[NavBar sharedInstance] release];

或同等学历。可能就在您退出之前。