0yeoj 0yeoj - 1 month ago 17
iOS Question

How to override self.navigationItem.backBarButtonItem Action?

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?

Answer

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)#>];