Tony Giaccone
Tony Giaccone

Reputation: 511

Why would adding a delegate to a UINavigationController crash my app?

I'm working on an iPhone application. I'm using Storyboard.

I wanted to have one object attached to the NavController to handle the visibility of the button bar at the bottom of windows that display table views.

I created my own UINavigationDelegate; MyNavigationControllerDelegate.

I have created an NSObject in UIBuilder and changed its class MyNavigationControllerDelegate. And then attached it as the delegate of the UINavigationController.

enter image description here

For some reason MyNavigationControllerDelegate is getting deallocated. I would have thought that being the delegate would be enough to keep a reference to it and prevent deallocation, but that seems not to be the case.

Any suggestions as to how to prevent it from being deallocated? And do I have to reset it as the delegate each time a view is loaded?

Upvotes: 0

Views: 237

Answers (1)

Matt Bridges
Matt Bridges

Reputation: 49385

The delegate property of UINavigationController is declared as assign rather than retain or strong (see here). Delegates are typically declared either weak or assign, which means assigning the property will not cause the UINavigationController to increase the reference count on your delegate, and you need another object to hold a strong reference to it (perhaps your RootViewController).

Upvotes: 2

Related Questions