I am setting the values of the title and back button in the UINavigationBar
as follows:
self.navigationItem.title = @"Post";
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [[UIColor blueColor] colorWithAlphaComponent:0.75f],
NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:20]}];
[self.navigationController.navigationBar.backItem setTitle:@"Pop Pop !"];
But i am unable to add target to the backbutton. I have tried the following:
[self.navigationItem.backBarButtonItem setTarget:self];
[self.navigationItem.backBarButtonItem setAction:@selector(goBack:)];
and
self.navigationItem.backBarButtonItem.target = self;
self.navigationItem.backBarButtonItem.action = @selector(goBack:);
But my selector is not being invoked on popping the viewController.
How do I add a target to the backButton, or invoke a selector on popping the viewController ? (I have tried all possible solutions mentioned in SO, in vain)
EDIT: I would to prefer to edit the behaviour of the stock backButton, instead of setting a custom button as the backButton.
Off of the top of my head. Try something like this
I have two solutions for you, giving that you cannot add a target or action to your back button:
1- Add a new barButtonItem and call it whatever and add your selector to it, and put it in the navigationItem.leftBarButtonItem. Don't forget to pop!
2- Override the viewWillDisAppear method, and do whatever you wanna do there.. if your viewcontroller doesn't direct you to another viewcontroller but the previous one...
if you have any more questions, just ask (Y)
You can not change
backBarButtonItem
action in such a way.I see here 2 possible solutions:
To implement you custom back button do whatever you want with its actions
Here is nice solution how to override standart back event and block
popViewController
when neededhttps://github.com/onegray/UIViewController-BackButtonHandler/tree/master
Yo can't add target to back button. You can add leftbarbutton item and addtarget to it.
First of hide back button
self.navigationItem.hidesBackButton = YES;
Then You make the custom button
AND add it to navigation left bar button item
Now add actions to it
You cannot add target to stock BackButton, but you can override a UINavigationBarDelegate method in your CustomNavigationController, like this: