Replace backButtonBarItem with leftBarButtonItem - ios

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.

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

Back Button Different on Different Navigation View Controllers

I have searched and can't figure out what is going on.
I have a NavController set up to root on VC1 which has a push segue to VC2. On VC2 the stock back button is just the Arrow Icon (no Arrow Icon with "Back Text).
In a different part of my app I have another Navigation Controller set up to root on VC5 which pushes to VC6. On VC6, the stock back button is the Arrow Icon with the "Back" text).
I am trying to be uniform but I can't figure out how to change these without loading in my own images (which I really don't want to do). I have tried to look for differences between the Navigation View Controllers but can't find how they are different.
Preferably I would just like the Arrow Icon without the "Back" Text.
Anyone experience this?
Another solution: You can control the text on the back button by setting the title of the view controller you came from.
self.title = #"my title";
And this text will appear on the back button of the next view controller you will navigate to. (Unless you defined an image for the back button)
This is not the best solution, (I prefer faviomob's solution using the storyboard, or programmatically), but it can be convenient in some cases.
Look at my picture. When I select any cell in the table view (in root view controller), the second view controller pushed with '111' back button. So, to have empty text there just set it to whitespace.
To set just the back arrow set the left bar Button item of the Navigation bar. Follow this:
//Set the back image. Use ur own image instead of back.png.
UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(leftBarButtonPressed)];
[self.navigationItem setLeftBarButtonItem:leftBarButton];

add back button to first screen of navigation controller in iOS

I know that a push segue automatically adds a back button so that the presentee can return to the presenter on click. What I want is for the back button to be available on the first UIViewController of the NavigationController (basically the only view controller in the chain that does not have a back button). How do I force add the back button?
I cannot simply add a custom image because I want the exact same chevron that comes with the iOS back button.
The easiest (yet hackish) way to achieve this is probably adding a dummy view controller in the stack before the actual first one, so you will get a proper back button. Use dummy's backBarButtonItem to customize the button title if you need, and -viewWillAppear: to respond to the button being tapped.
What I want is for the back button to be available on the first UIViewController .... How do I force add the back button
This whole thinking and logic is wrong. This is not how iOS UINavigationControllers work. You have a starting page (mainViewController) in a UINavigationControllers and from there you "move on" to something else. But you always come back to the main view controller. Just think about it, if you put a back button on the main view controller, where will the user go back to?
If you have a situation like this.
SomeViewController --> UINavigationController --> MainViewController
Once user is on SomeViewController and you take him to MainViewController which is part of UINavigationController then back button makes sense.
Just a FYI - you can easily drag and drop a UIBarButtonItem to any UINavigationController in Xcode and call it "Back". You will have to then connect that back button to another view controller.
Maybe it doesn't make sense but why can't you add a custom image? If all you want is "Back" chevron, can't you take a screenshot of this button and then put this image on your custom UIBarButtonItem?

How to manually add a Back button to my NavigationBar?

I would like to know how I can add a back button to my UINavigationBar, I know that If I embed in a Navigation Controller in my main view, that all of this would happen automatically, but that is not what I am trying to do, in fact, the back button could go to any other view I connect the segue to, doesn't even have to be the back.
The back button needs to be the system navigation "back" button, I don't want to have to set image files for my buttons.
I tried dragging in a bar button item in storyboard but nothing shows up, doesn't seem to be doing the trick. The navigation bar was dragged to the view in storyboard (not created programmatically) and it shows up just fine.
I looked at the following similarly asked questions:
-> How do i add a 'back' button to a UINavigationBar manually?
-> How to add a button to UINavigationBar?
-> How to programmatically add a UINavigationBar and a back button on it
but they all point to just embedding the view in a navigation controller, which again, is not what I am trying to do.
If someone can please help me out, provide some sample code I would greatly appreciate it.
Try drag-drop a UIButton object. I have always added manual back button (hardly took the in-built one)

Replace left navigation button with custom button, then back

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];
}

Resources