0

I am trying add a custom back button so that I can perform some tasks before popping the controller. But no matter how hard I tried this function bind with back button doesn't work. I am adding some code to understand

-(void)customizeNavigationBar
{
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"Video Call";

   UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
              initWithTitle:@" " style:UIBarButtonItemStylePlain 
              target:self action:@selector(didBackBtnTap)];
   [self.navigationItem setBackBarButtonItem:backButton];
}

and this is the function, which is not calling at all,

-(void)didBackBtnTap
{
    [self popToProfileScreen];
}

Please suggest any solution, Thanks in advance.

Najam
  • 1,129
  • 11
  • 32

4 Answers4

1

Use leftbarbutton item instead of setBackBarButtonItem

self.navigationItem.leftBarButtonItem = backButton;
Hitesh Agarwal
  • 1,943
  • 17
  • 21
  • I want it to look like back button but override it with my custom function .. I don't want to add any icon with it – Najam Jul 07 '18 at 13:17
1

When you use the setLeftBarButtonItem or setBackBarButtonItem, it will take effect on the viewController you’re pushing to but not the current one. That’s why you need to directly set the current leftBarButtonItem

congsun
  • 144
  • 5
0

Another option is to write your custom code in viewWillDisappear.

Aamir
  • 83
  • 1
  • 3
0

Use this code

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 15, 15);
[backButton setImage:[UIImage imageNamed:@"backButtonImage"] forState:UIControlStateNormal];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Add an image(15*15, @1x @2x @3x) like back button of NavigationBar in xcasstes backButtonImage

- (void)backButtonPressed {}
Sid Singh
  • 53
  • 7