Replace left navigation button with custom button, then back - ios

I have a UITableView within an UINavigationController. On the right of the UINavigationBar I have an "Edit" button. When I tap this button, all the textfields in the table cells become activated so I can edit them. The right button changes to "Save".
I'd now like the left "back" button of the UINavigationController to be replaced by a "Cancel" button which when I tap doesn't bring me back to the previous UIViewController, but just cancels editing mode.
When I tap "Save" the "Cancel" button should be changed back to the usual UINavigationController back button. Is there any easy way to do this? I tried accessing [[self navigationItem] leftBarButtonItem]. This works for the right button, but not the left one.

Set the leftBarButtonItem to your Cancel button. At the appropriate time, set the leftBarButtonItem to nil to remove your Cancel button. This will automatically restore the original back button that was in place before you added the Cancel button.

It sounds like the leftBarButtonItem is a "BackButton", which is set on the UIView(Controller) you used to navigate to the current View.
You can hide the BackButton following the answer in this question. This should be done, since you cannot change it's behavior.
I believe you are then able to use the leftBarButtonItem.

See this other SO question.
Then make sure you declare the cancel method similar to this.
(void)cancel:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}

Related

Back bar button item with right button

I have implemented a right item button in the navigation bar. I use the storyboard, no code. But now the back button is not displayed. What's the good way to show it again without boilerplate code ?
Thank you!
You can not see the back button on the rootViewController of the NavigationController
NavigationController automatically add the back button once you push some other ViewController to it.
It will display if you have not hides it explicitly.
If you have drag and drop the BarButtonItem as shown in the attached image, Just run the code and back button will automatically get added if it was not your RootViewController.
If it is RootViewController as displayed in images, try to push some ViewController on it, you will get the back button added.
If you add the left bar button item from Interface Builder, you will not get the back button displayed. Then you have to do it manually

Detecting when back button is tapped in navigation bar

I need to resignFirstResponder() on a text field when the user taps the back button in the navigation bar of the navigation controller, otherwise I get some error. The back button works as it should (the previous view gets shown) but I don't know where to do resign first responder. It's too late if I do it in viewWillDisappear() (I tried), and prepareForSegue() doesn't get called, so I need to somehow do it as soon as the back button gets tapped or at least before viewWillDisappear(). How do I detect that event?
Note: See first comment on question for simpler answer
You could combine NotificationCenterand this post: Execute action when back bar button of UINavigationController is pressed to make a custom back button (that looks the same). Just post a Notification in the action for the back button, and add an observer for the Notification on the textfield whose action calls resignFirstResponder. You would also need to make an image or draw an image in CoreGraphics for the arrow.

Back button not appearing on navigationcontroller

Above is my storyboard. As you can see, view a and view b are both on the same navigation controller. They are both show segues. The segue to View B is activated manually by a button on the subview on the view of the previous "Home" controller and that works perfectly and has a back button. Here's the code:
- (void)storyButtonPressed:(UIButton *)button {
[self performSegueWithIdentifier:#"showStory" sender:self];
}
The segue to view A is activated manually by an if statement to see if the user is registered:
if (currentUser.active != true) {
[self performSegueWithIdentifier:#"showSignUp" sender:self];
}
However view A will not show up with a back button even though I'm going about it just like view B. I even tried to add a button to the navbar and manually pop off the view but that button won't even show up! Here's my document outline:
How do I get my view A to have a back button or at least allow me to add one manually?
Thank you.
Are you sure you the segue to A is set to "Show" and not to "Show Detail"?
Also the cancel button in your navBar looks like it might be covering up your button. You might try pinning your cancel button to the right side of your navBar and then seeing if it shows up with a back button.

How to change a navbar UIBarButtonItem button to a navbar back button?

I have a UIViewController with a navigation bar i dropped in in storyboards. Then I dragged in a UIBarButtonItem which is rectangular.
I want that button to look like a navigation bar's back button
I was suggested to do this:
self.navBar.topItem.backBarButtonItem = self.button;
After properly creating the properties and connecting them to the outlet. I did but the button still looks like the Done Rectangular button. How can I change it?
Those buttons are created automatically when you do a push segue.
For your particular problem, you can call perform [self dismissModalViewControllerAnimated:YES] in the case of modal segues, or [self.navigationController popViewControllerAnimated:YES]; for dismissing custom push segues.
Anyway, I don't know any method to change the button style to the "left arrow", other than selecting a background image with your particular "button".

Replace backButtonBarItem with leftBarButtonItem

I think this is a really simple task, but I just can't get it.I have a viewcontroller on a storyboard that isn't very complicated. Here's what I want to do:
I want the navigation bar of my view controller to have essentially 3 buttons, but only show two at a time. One button is a simple uibarbuttonitem that is always on the right side. Another is a backbuttonitem that's always on the left side. Lastly I want a save button to be on the left side also. I want this save button to appear in place of the back button only when a uitextview is being edited and then have the back button appear again in place of the save button when the textview is done being edited or when the save button is clicked.
Anyone know the easy way to do this? Do I do it through the storyboard, or should it be done completely programmatically?
I don't know about storyboards but in code this is trivial. To show the "save" button on left you create the button and call:
self.navigationItem.leftBarButtonItem = saveButton;
When you wish to remove the "save" button and show the back button again, you simply do:
self.navigationItem.leftBarButtonItem = nil;
This code goes in your view controller (self) and it assumes the view controller has been added to a navigation controller.

Resources