Umitk Umitk - 5 months ago 9
Objective-C Question

How to override navigation controller after it is pushed?

I want to override to my own navigation controller with its own back button, title etc. But after i pushed the view controller, by default shows the navigation bar of the last view in the stack which was inherited in previous class.

Lets assume this is my first class which inherits NavBarViewController in header file as;

//ListingDetailViewController.h
@interface ListingDetailViewController : NavBarViewController


and, inside ListingDetailViewController, i pushed view by:

//ListingDetailViewController.m
ChatListViewController *chatList= [[ChatListViewController alloc]init];
[self.navigationController pushViewController:chatList animated:YES];


So this will still bring in the navigation controller same as in ListingDetailViewController, even though i tried to override the back button, title again in ChatListViewController's viewdidLoad and viewWillAppear. This is ChatListViewController's header:

//ChatListViewController.h
@interface ChatListViewController : IPChatListViewController


How can i achieve this? Any help is much appreciated. Thank you.

Answer

I found the solution by overriding the button in viewDidAppear, because until viewDidLoad it will re-use the inherited navigation bar. So,

-(void)viewDidAppear:(BOOL)animated {
[self overrideBackButton];
}

Then:

- (void)overrideBackButton {
UIImage *backNormal = [UIImage imageNamed:@"backButton"];
_navBarLeftButton = [OpenSansButton buttonWithType:UIButtonTypeCustom];
_navBarLeftButton.frame = CGRectMake(10, 10, 35, 35);
[_navBarLeftButton setImage:backNormal forState:UIControlStateNormal];
[_navBarLeftButton addTarget:self action:@selector(backToList) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:_navBarLeftButton];
}

And it is triggered by:

-(void)backToList
{
[self.navigationController popViewControllerAnimated:YES];
}