8

I want to override self.navigationItem.backBarButtonItem's target and action,

I tried:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(backButtonOverrideAction:)];
[self.navigationItem setLeftBarButtonItem:backButton];

it is working but i want to use the default arrow:

enter image description here

i also tried:

UIBarButtonItem *backButton = self.navigationItem.backBarButtonItem;
[backButton setTarget:self];
[backButton setAction:@selector(backButtonOverrideAction:)];

but unfortunately it's not working.. do you have any idea how to do this?

0yeoj
  • 4,500
  • 3
  • 23
  • 41
  • 1
    What different action do you want? The back button has a specific action that users expect, so changing it is not a good idea. – rdelmar May 29 '15 at 06:12
  • Yes, i know but i'll be like a back button of a browser.. Even `BarButtonSystemItem` dont have the icon for back button.. tsk.. Apple.. – 0yeoj May 29 '15 at 09:17

8 Answers8

13

STILL, No direct access for changing target and action on self.navigationItem.backBarButtonItem.

I ended up using the first one i tried... and get my (custom) image..

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(backButtonOverrideAction:)];
[self.navigationItem setLeftBarButtonItem:backButton];

while...

//this two lines of code are still useless, i just feel sorry for them.. 
[self.navigationItem.backBarButtonItem setTarget:<#(id)#>];
[self.navigationItem.backBarButtonItem setAction:<#(SEL)#>];
0yeoj
  • 4,500
  • 3
  • 23
  • 41
  • Worked like a charm. Thank you! – DocAsh59 Nov 22 '17 at 12:38
  • 1
    Be aware that using the leftBarButtonItem as a back button causes the `UINavigationController.interactivePopGestureRecognizer` to stop working. If this is a concern see [this question](https://stackoverflow.com/questions/34942571/how-to-enable-back-left-swipe-gesture-in-uinavigationcontroller-after-setting-le/43433530). – slythfox Aug 08 '20 at 02:15
4

You cannot assign action to back BarButton. The solution for that is to create the custom back button.

Dhiraj
  • 75
  • 3
2

In swift 3.1

override func viewDidLoad() {    
// set navigationItem.leftBarButtonItem return to Root
let backToRootVCButton = UIBarButtonItem.init(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(backToRootVCAction))
self.navigationItem.setLeftBarButton(backToRootVCButton, animated: true)

}

func backToRootVCAction() {
    _ = self.navigationController?.popToRootViewController(animated: true)
}
Jayce
  • 427
  • 5
  • 6
1

This works for 11.3:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let BackItemIndex = 2
    if let subViews = navigationController?.navigationBar.subviews, 
        subViews.count > BackItemIndex {
            navigationController?.navigationBar.subviews[BackItemIndex].subviews.first?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(doPerformAction(_:))))
    }
}

@objc func doPerformAction(_ sender: Any) {
    // some code
}

This solution is based on searching the active subviews until the corresponding to backBarButtonItem and then add a UITapGestureRecognizer, so that it captures the event on the element.

Jose Gil
  • 11
  • 2
1

"backAction" (new in iOS16)

If a back button already appears in the navigation bar, setting this property replaces its action without modifying its appearance. Otherwise, setting this property generates a back button with the image or title from the action you specify, unless you use the UINavigationItemStyleEditor navigation style.

https://developer.apple.com/documentation/uikit/uinavigationitem/3987966-backaction?language=objc

benc
  • 1,381
  • 5
  • 31
  • 39
0

Excerpt from Apple Doc says:

When specifying buttons for a navigation item, you must use UIBarButtonItem objects. If you want to display custom views in the navigation bar, you must wrap those views inside a UIBarButtonItem object before adding them to the navigation item.

That means you simply cannot use the method-2 from your question. Whereas method-1 is the right way to do it.

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
0

I hope it helps you...

BOOL IsOK_Clicked;

-(void)viewDidAppear:(BOOL)animated
{
 IsOK_Clicked = NO;
}
-(void)viewDidDisappear:(BOOL)animated
{
   if(!IsOK_Clicked){
    //Do what you want in Back Click
  }
}
- (IBAction)btnCheckOut:(id)sender {
 IsOK_Clicked = YES;
}

Or You may add left bar button item to navigation bar and set your own selector function to it.

Mohamed Jaleel Nazir
  • 5,776
  • 3
  • 34
  • 48
-1

You have to do something like

self.navigationItem.backBarButtonItem.target = self;
self.navigationItem.backBarButtonItem.action = @selector(backButtonAction:);

here you have to handle the pop for the controller also.

Retro
  • 3,985
  • 2
  • 17
  • 41