Greg
Greg

Reputation: 34798

could strange UINavigationController nav bar behavior be due to memory leaks?

I'm getting strange navigation bar behaviour, for example when I hit back button the screen displayed is the previous screen, however the Navigation Bar items do change. So I'm left with screen A, but with nav bar buttons for screen B.

Could this be due to memory leaks? I do note with my app still:

  1. This behavior seems to happen:

    • immediately if I trigger memory warning via the simulator menu, or
    • on a device after it has been on for a while [without being killed and then restarted as an app].
  2. I do have some memory leaks I'm trying to clean up (i.e. Profiler highlights items in "leaked blocks" section)

Any tips on fault finding root cause of why pushing a back button would end up in a weird state? e.g. screen on previous parent view, but nav bar items don't change...

UPDATE - I have finally removed the memory leaks in my app, however I note the nav bar issue still remains. I guess this doesn't confirm the answer to my question is NO in general, but in my specific case it wasn't the memork leak...

Upvotes: 0

Views: 459

Answers (3)

lemnar
lemnar

Reputation: 4053

I've seen something like this happen after calling -[UINavigationController setViewControllers:]. You might try not doing any programatic manipulation of the navigation controller's view controller beyond calling -[UINavigationController pushViewController:animated:].

Upvotes: 0

BadPirate
BadPirate

Reputation: 26177

Any view that is not currently visible and only retained by it's view controller (as a part of the view property of that view controller) will be released (along with any non retained subviews) when a memory warning occurs.

Chances are you are creating the view as a part of init, and not retaining it in the controller (simply letting the view socket hold it from releasing). One way around this is to create properties for the views you create (nonatomic, retain), and after creating them and autoreleasing, assign to those properties, don't forget to assign those properties to nil as a part of dealloc to avoid leaking. Another way is to create your custom view elements in viewDidLoad as opposed to init.

Hard to say without code example from the offending views :)

Upvotes: 0

octy
octy

Reputation: 6545

From Apple:

The navigation controller updates the navigation bar each time the top view controller changes. Thus, these changes occur each time a view controller is pushed onto the stack or popped from it. When you animate a push or pop operation, the navigation controller similarly animates the change in navigation bar content.

Based on this, I would start by looking for a bug or misconfiguration in your view definitions. Check for any InterfaceBuilder warnings if you defined your views via NIBs. Make sure your view hierarchies are correct in both UIViewControllers. Also check for possible bugs in your view life-cycle methods: viewWillAppear:, viewWillDisappear:, etc,.

Actually, it would be nice if you could post some screenshots and/or code. Thanks!

Upvotes: 1

Related Questions