Reputation: 1656
I am creating a view controller that is pushed to the UINavigationController
. I need a customized navigation bar, therefore I have overridden navigationItem
method of my view controller. In a titleView
(property of UINavigationItem
) I need a custom view that holds two buttons. Question: how to apply view controller for those two buttons?
View containing two buttons is defined in CustomTitleViewController.xib
, whereas its view controller is defined in CustomTitleViewController
class.
This is my way of returning the navigation item (MainViewController.m
):
- (UINavigationItem *)navigationItem
{
UINavigationItem *navItem = [[UINavigationItem alloc] init];
UIViewController *customTitleViewController =
[[CustomTitleViewController alloc] initWithNibName:@"CustomTitleViewController"
bundle:[NSBundle mainBundle]];
navItem.titleView = [customTitleViewController view];
return navItem;
}
The view is shown on the navigation bar, as expected. However, tapping the button crashes the application (EXC_BAD_ACCESS
).
Error message: message sent to deallocated instance 0x6e53850
.
Any ideas?
Upvotes: 0
Views: 561
Reputation: 2272
Basically, you need to retain pointer to viewController as well as to its view. Just create strong property on your UINavigation controller subclass. Coz what you are doing here is instantiating new controller each time navigation item is called and realising it at the end of the function. Here is a very quick crude fix: https://www.dropbox.com/s/y6ltdyj951ioncd/Navigating.zip be sure not to reinstantiate VC all the time and keep pointer to it. Hope this helped.
Upvotes: 1