views:

36

answers:

2

Hi,

I have a navigation based iPhone app. When you press on a cell in the tableview a new UIViewController is pushed to the navigation stack. In this view controller I am setting a custom titleView in the viewDidLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Setup custom navigation title
    [self setTitle:@"Mediaportal"];
    navItem = [[NavigationBarTitleItemViewController alloc] initWithNibName:@"NavigationBarTitleItem" bundle:nil];
    [navItem setTitle:[theServer name]];
    [navItem setSubTitle:@""];
    [self.navigationItem setTitleView:navItem.view];

…
}

Once I switch back to the RootViewController:

[self.navigationController popToRootViewControllerAnimated:YES];

the app crashes with the following error (NSZombieEnabled=YES):

*** -[CALayer retain]: message sent to deallocated instance 0x5a5fd80

From what I can see the RootViewController still tries to access the custom titleView, which was deallocated with the second view controller. Once I comment out the custom titleView part in my code the app works. I tried to set the navigationItem.titleView to nil (as found in the apple docs) before the second ViewController is deallocated, but that doesn't help.

Do you have a hint what I can do to prevent this crash?

Thanks, Mark.

A: 

I had this exact same error a month or so ago, exactly the same situation. It drove me NUTS.

I discovered that the viewController i was popping too hadn't been deallocated at all. I had a custom UIButton subclass added to that view however that had been deallocated when the second view had been pushed. So when popping back, the UIButton wasn't there.

Check the view you are popping back to, to ensure you've not got any classes that you are deallocating, or are being autoreleased without you knowing.

Hope this helps.

Bongeh
Hi Bongeh,thanks for your answer, but I think I am having a different problem.The crash does not occur when I don't add the custom title view to the navigationItem in my subviewcontroller. Therefore I am pretty sure that the problem is not some deallocated object in the root view controller? Or am I missing something there?
Mark Gibson
Without seeing more of your code or a simplified project that replicates the problem i can only throw pot shot guesses at the problem. Obviously we know something is being deallocated that shouldn't be so.Are there any classes or objects in NavigationBarTitleItemViewController that are being released, or are autorelease objects?
Bongeh
A: 

I finally found the solution for it (a quite simple one). I have to alloc and init the navItem through its property then it is being retained:

self.navItem = [[NavigationBarTitleItemViewController alloc] initWithNibName:@"NavigationBarTitleItem" bundle:nil];
Mark Gibson