Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

iphone - IOS 4.3 UINavigationBar tintColor Leaks

In IOS4.3 if I set

navigationBar.tintColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];

I will get a memory leak: UIDeviceRGBColor leak

But if I use navigationBar.tintColor = [UIColor blackColor]; Everything is fine.

This never happened in ios4.2

I did some debug, and I found the [navigationBar.tintColor retainCount] seems bigger if I use

[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];

Does anyone have the same issue?

This is the leak code:
In RootViewController:

- (void)viewWillAppear:(BOOL)animated { 
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        [super viewWillAppear:animated];
    } 

In DetailViewController:

- (void)viewWillAppear:(BOOL)animated {
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.9 green:0 blue:0 alpha:0];
        [super viewWillAppear:animated];
    } 

If you go to DetailViewController, then popback to RootViewController, in the Instruments, you can see the UIDeviceRGBColor leak

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ive had this issue before 4.2, i think colourWithRed:Green:blue allocates a new UIColor object which your responsible for managing.

The solution is to create an instance for your tint colour and release it when your done with your navigation controller in viewDidUnload.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    tintBarColor = [UIColor   
                colorWithRed:50.0/255   
                green:134.0/255   
                blue:187.0/255   
                alpha:1];
    self.navigationController.navigationBar.tintColor = tintBarColor;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [tintBarColor release];
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on     demand.
    // For example: self.myOutlet = nil;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...