What exactly does the back button in Navigation controller do? - ios

Is there anyway to tell what specifically the back button from a navigation controller does? I know in terms of what it does, but in terms of method called/functions etc.
The reason is that I am hiding the navigation bar and will have a button on view instead and just want to replicate this completely.

If you want to replicate the back button feature in navigation bar, you need to call the following in that button's action:
[self.navigationController popViewControllerAnimated:YES]

In your view controller call this, when your custom back button is tapped:
[self.navigationController popViewControllerAnimated:YES];
This will pop the current view controller.

This is the UINavigationController method that the back button calls:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
So to replicate it just do something like this in the viewController you are popping...
[self.navigationController popViewControllerAnimated:YES];
You can verify that this is exactly the method called by the back button by subclassing UINavigationController and overriding this method - you will see that it gets called when the back button is pressed (no need to do this in your app - it's just a way for you see what is going on!)

If the you're referring to the leftBarButtonItem and not the built in backButton, then it can do whatever you want. It does not automatically dismiss the current view controller. You need to hook up a selector to the barButtonItem for it to do anything. When that action is fired, you tell it what to do whether it be pop, segue, or virtually anything.

Related

ViewController pushing to next according to the specific notation

I need to push and pop the ViewController accordingly, but while pressing back button in my application, the ViewController should switch to next ViewController in backward. I. e. normally by pushing ViewController it slides from right to left, but while clicking back, the ViewController should come from left to right.
I normally use simple presenting and push ViewController codes. Please help me. I don't have navigation controller in my application for some pages and some pages have. Please provide me both solutions.
Though your question is not clear, I am assuming you want animation when transitioning back to previous view controller.
If you are not using navigation controller, just dismiss you controller like:
[self dismissViewControllerAnimated:YES completion:nil];
In case of navigation controller use this:
[self.navigationController popViewControllerAnimated:YES];

Going back to main view using the navigation bar back button

My app has a main view with a few buttons lined up vertically. Each button is linked to a different view controller. Now, when the user taps on the first button, the first view controller is opened. Within the first view controller, there is a button which takes you directly to the second view controller. When I get to the second view controller, and press the back button, I am taken back to the first view controller. Then I have to press the back button one more time to be taken to the main view controller. What can I do to make the app take me directly to the main view controller from the second view controller, and not through the first view controller? Thanks in advance!
You could bypass the segue_unwind to go back to the main view controller. Just call the performSegueWithIdentifier in the IBAction of the button
Swift
self.performSegueWithIdentifier("SegueIdentifierName", sender: self)
Objective C
[self performSegueWithIdentifier:#"SegueIdentifierName" sender:self];
Here is a similar article as well
What are Unwind segues for and how do you use them?
You can override the back button action to call popToViewController() on the navigationController with the view controller you would like to return to.
There are a lot of answers in SO about this BUT:
[self.navigationController popToRootViewControllerAnimated:YES];
OR
UIViewController *rootViewController = [self.navigationController.viewControllers firstObject];
[self.navigationController pushViewController:rootViewController animated:YES];
Should work just fine.
Next time, TRY to find answers first:
How do I get the RootViewController from a pushed controller?
SWIFT 3.01
self.performSegue(withIdentifier: "SegueIdentifierName", sender: self)

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?

UIBarButton does not go back to Menu

This may be a simple answer, but I am new to this... my back button in the navigation bar is confused and does not want to go back to the Main Menu. Instead, it goes back to the previous view. How can I specify in my app that the "back" button should always go to to a specific view?
All you have to do is create a UIBarButton and connect an outlet to it. In the action:
-(IBAction)MainMenu
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
If you want to go back to the root controller:
popToRootViewControllerAnimated:
Pops all the view controllers on the stack except the root view controller and updates the display.
[self.navigationController popToRootViewControllerAnimated:YES];
If you want to go back to an specific controller:
popToViewController:animated:
Pops view controllers until the specified view controller is at the top of the navigation stack.
You will have to create a new outlet in your view controller to handle the button press. Then use the code NSElvis posted in his answer to pop to root.

ios custom back button (NOT in the navi bar)

i'm building my own navigation between views and i have the navi bar hidden by purpose. still, i need a button inside the view (read outside the navi bar) that would behave like "back" button. is there such method to call that could be assigned to a custom button?
i tried standard push segues, but looping back will create new instances and I just need a standard back behavior (that will destroy the current instance).
i guess it's obvious, but somehow i've been missing it. thanks.
Create a button as normal in your view, then connect it to a method that pops the view from the stack. Something like this:
- (IBAction) pressedBackButton:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
}

Resources