Let's say we've got a few UIViewControllers on the stack of our UINavigationController
How can I define the UIViewController (instead of the previous one) the back button should pop to? (so basically pop two or more instead of one)
Maybe I want the back button to pop back to the root view controller, or just 2 view controllers before.
I know I could create a custom left bar button, but that looses the nice leftwards arrow in the button. And I don't want to use a png graphic to get that arrow back.
Edit:
I want to know how to pop to a certain UIViewController when pressing the back button in the navigation bar. I know how to pop to a certain controller, but not how to have the back button do it.
When you are pushing the viewControllers push all the ones that you may want to go back to and then use popToViewController:animated: to go back to the one that you want. Or use popToRootViewControllerAnimated: to go back all the way to the rootViewController.
Related
I am trying to add a Back button onto my table view controller (far right VC in the picture). I embeded a Navigation Controller and added a Back Item on the navigation bar with a segue to my second VC. However when I run the code, after I press back on the TableVC, it goes to a blank screen with the Back button still at the top. I want to be able to have the second VC screen when the back button is clicked, but I think there is some funky thing the navigation bar does. I was wondering if there was a way around this.
Ok so I'm pretty sure your problem is your embedding the wrong controller in the nav controller. You want to embed the second view controller in your picture into the nav controller, and then have your last one segue from that view controller.
Also, with a nav controller you don't need to add a nav bar or the back button. The embedded controllers will have these by default.
Embedding controllers is kinda confusing at first so its a good idea to take a look at some additional docs.
Here's a good tutorial that I used when starting out: Tutorial
I just started with Xcode and Swift.
I try to build my first little App for iOS. But now I have the problem, that I don't know how to implement a the back button, so that i come back to the view before.
My Storyboard look like this:
When I open the A-Z view, I want to display the Back Arrow, which turn me back to the Item 2 view.
To open the A - Z view I connect the button "Medikamente A - Z" with the Navigation Controller.
When using storyboards the back button is usually implemented with unwind segue.
I usually like to follow raywenderlich toturials on UI related topics, like this - http://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2
It include a detailed example of how to implement back button in storyboards. Quoting from it -
Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next.
There are three main steps:
1. Create an object for the user to select, usually a button.
2. Create an unwind method in the controller that you want to return to.
3. Hook up the method and the object in the storyboard.
When using UINavigationController, whenever you push to a new ViewController the back button will automatically appear, so you can jump back to the previous View Controller.
So it's works like:
UIViewController -> UIViewController -> UIViewController
A back button will appear on the last 2 so you can pop back the the previous ViewController.
You don't have to do any additional coding for the back button to appear, it'll do it on its own. I hope this clears it up. Let me know if you have any questions.
To implement a back button, your root view controller has to be a Navigation Controller.
The first view controller becomes the navigation root of the navigation controller.
If you want to present another view controller, you select a "Show Detail" relationship as the action for the button which should show the view controller. To do this, Ctrl-click and drag from the button to the destination view controller and select "Show Detail".
I had the same problem, even when on the storyboard the back button was visible at design time.
I deleted the segue, and recreated it with "Show" instead of "Show detail". Changing the segue to "Show" had no effect. I think this is a bug, so if you miss that back button delete and recreate the segue.
Suppose I have 3 tabs on tab bar controller:
TabAViewController
TabBViewController
TabCViewController
and in tab C I have one button.
When I click on that button then open the another View (CsubViewController which is not TabAViewController, TabBViewController, TabCViewController). How to call that Another View (CsubViewController) on button click?
And how to come back to the same view after back button click.
It depends. If you are using a NavigationController as the root controller, you push the CSubViewController in your current ViewController by
navigationController.pushViewController(...)
and go back with
navigationController.popViewControllerAnimated(...)
which is the simplest way to achieve your goal.
Or you can write your own code and try to present the CSubViewController modally in traditional presentViewController(...) and dismissViewController way.
I have an app that has a toolbar, but I don't want the bar at the top, in order to free more viewing space. Therefore I have decided not to use a navigation controller. I'd like to add a back button to the toolbar. How would I go about this?
Adding the button is easy enough, and setting the action to performSegueWithIdentifier is all fine, but what happens is that the previous view just gets loaded again, rather than show it as it was, like a true back button. So if I tap on the 10th row on a tableView and go to a new page, when I press the back button it loads the view from the top again, instead of showing it as where I scrolled down to last.
Even though you don't want a UINavigationBar, you do want a UINavigationController in this case, because it manages the 'back stack' exactly the way you want it. Just hide its navigation bar by setting its navigationBarHidden property to true (in the Storyboard or in the viewDidLoad function of the root view controller).
You can then use navigationController.popViewController(true) as normal, in response to the user clicking your custom back button.
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?