Search code examples
iosobjective-cuinavigationcontrolleroverridingpushviewcontroller

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.


Solution

  • 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 overwriteBackButton];
    }
    

    Then:

    - (void)overwriteBackButton {
    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 to:

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