Search code examples
ioscode-analysisnavigationcontrollernavigationitem

Releasing a navigationItem


I've the following question:

I've an AppDelegate and I add there a navigationController and load UIViewController B in it.

In B I add a navigationItem, a leftBarButtonItem or a rightBarButtonItem.

Where do I have to release these items, because I alloc and init them in B. So at the first I thought about releasing self.navigationItem.rightBarButtonItem in Dealloc-Method of B.

But if I'm analyzing my app, the analyzer says at the release-position in B's dealloc-method:

Incorrect decrement of the reference count of an object that is not owned at this point by the caller.

But I don't understad what I've done wrong or is everything ok and it's an analyzer problem?

Can somebody help me finding out?

Greets andi1984


Solution

  • As soon as you allocate and assign to rightBarButtonItem you can release like

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showNewEventViewController)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    [rightBarButtonItem release];
    

    Similarly to leftBartButtonItem

    self.navigationItem.rightBarButtonItem might have a retain in itself and knows when to release that count.