I am designing two pages, one followed by the other, in Xcode 7. Let's call them the first and second view controllers. From a button in the first view controller, with a modal segue, the second view controller shows up. I want to add a button to second view controller so that when I hit that button, I can simply exit from the second and back to the first view controller.
I know this feature can be done by navigation controller with putting cancel button to the navigation bar on second view controller which I don't want to. I want specifically a button to have that functionality.
I thought of calling the first view controller with code when the button is tapped, but it sounds me like a bad coding. So, I would like to learn what is a good way of achieving this.
Thanks,
Add method to the second view controller:
#IBAction func exitButtonPressed(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil);
}
Next add button to the second view controller in interface builder and connect button's action to this method.
use Unwind Segues
Unwind Segues give you a way to “unwind” the navigation stack and specify a destination to go back to.
for sample tutorial1, tutorial2
Related
I am interested in implementing a back button to segue to a previous view controller. I have tried to embed a navigation bar onto the the top of the table view controller but the issue is when I do segue back to the first view controller, by default, Xcode keeps the navigation bar with a back button to go back to the table view controller. Is there a simpler way to implement a segue to go back to the first VC without the navigation bar remaining?
I'm not too sure if this works, but embed your view controllers including the first one inside the navigation controller. That would make all your view controllers with navigation bar above.
On the main view controller (the one you do not want to have the navigation bar), add the line of code inside your viewDidLoad method.
Swift 3:
self.navigationController?.navigationBar.isHidden = true
I found an easy way. On your TableViewController, drag a UIview to the top of the view controller and itll let you insert the view. From there just add your back button
Just assign your "Back" UIBarButtonItem to this method and it should work.
#IBAction func backButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Sounds like a few problems
1) How are you navigating from the first view controller? Is it with Show (Push) if not, you are replacing the first view controller in your stack.
2) Based on your problem, make sure your first view controller is not embedded in a navigation controller
3) As Dylan mentioned, you need to hook your back button to remove the current view controller to return to the first one
4) Extension of (3), how are you segueing back to the first view controller? If you are explicitly navigating to first view controller with your back button handler, it's not actually going back but rather forward and keep the navigation bar.
i'm doing an app that uses a TableViewController with a system of Adding/Editing items, using the same view. (however when you add an item the view is modal presented and when you edit it is shown)
I followed the great starter tutorial from Apple so basically my Storyboard looks like this
The segue going through the Navigation Controller is for adding and the other one is for editing. (I did everything according to the tutorial).
I did a segue between the Cancel item bar button and the exit icon of the ViewController and it works well when the view is modally presented (when I try to add an item).
However when I click on a cell to reach the view with the segue that shows it (to edit an item), both items in the navigation bar stop working. The prepareForSegue method is not called anymore. So I can't cancel or save.
I tried creating an unwind segue between the ViewController itself and the exit icon and to call it programmatically like this:
#IBAction func testButton(sender: UIBarButtonItem) {
print("we're inside")
self.performSegueWithIdentifier("cancelSegue", sender: self)
print("so what now")
}
and when I try to edit it and tapping the cancel button it results by just showing the two log messages and kind of skipping the performSegueWithIdentifier method. However adding still works fine.
Am I doing something wrong or have I misunderstood some basic notion about unwind segues?
This seems like quite a strange solution to your problem. While I can't comment on specifically why your unwind segue isn't working in the second case, things will start to get quite complicated with dismissing the New Programsegue since you're displaying it two different ways.
The common approach that we use is:
Use two completely different view controllers for creating and updating an assets. May be some duplicate code, but makes it slightly easier for other people to work on.
Use the same view controller for creating and updating buttons. If you're editing an item, you can pass it to the view controller with prepareForSegue. When the view controller loads, if an item is present, you can change the behaviour of the buttons, title etc. If no item is present, you know to create a new item.
For most implementations, it's much simpler to dismiss the views programatically. (i.e [self dismissViewControllerAnimated:NO completion:nil] for modal views and [self.navigationController popViewControllerAnimated:YES] for just returning to a vc in the same navigation controller stack.
Update
Did you link the Cancel button to the exit icon on the first view controller, or the second view controller (the one with the cancel and save button)?
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.
I want to design a routing mechanism for management view controller transitions in objective-c.
Two view controllers, without reference to the other side of the pointer, the transition is completely controlled by the route.
How to achieve the route?
Thanks for your help.
I hope you think like this.
Route = Segue (In Xcode)
Take Two View controller
Make 1st Embed in Navigation controller (Which is must be initial View Controller)
Put One Button in 1st VC
Press Ctrl + Drag Mouse from button to 2nd VC (One popup will be show, then select Push)
Run Project
Press Button
Lol
You can use Storyboard's segues. Create view controllers and segues between them. You can use Push Segue or Model Segue. Push Segue will let you pop next view controller too, and you will have to embed your first view controller into navigation controller. For Model Segue, you can move from one view controller to another but to return to previous view controller, you will have to create another segue.
I think I find the approach to answer this question。 Here is the similar project.
I have an iOS app with 3 views. I am using a "push segue" to move between them. I have linked a push segue from a button in First view to Second View. For moving from second to third, I cannot have a button. I have a list of items and the person can click in any of those to move to third(and last) view. So I added a segue in storyboard and calls it programmatically. The third view is opening properly except that the nav-bar is no longer there.
[self performSegueWithIdentifier: #"moveToFilesSegue" sender: self];
I can live without nav-bar. But the real problem is that I cannot do a push segue from my third view to any other view.
So my question is whether there is a way to call a segue from second view to third view in such a way that the third view also has the navigation controller that the first two have by default(ie navbar is also visible in third segue). There is some break in navigation because of me calling a segue programmatically.
So a few comments. If you would like to be able to get to the two other views from any view at any point in time, then I would suggest using a tab bar controller rather than a navigation controller.
If you must use a navigation controller, then you should keep the first view controller as the root view controller. Imagine then tapping a button that pushes the second view controller. Then say you tap a button to get to the third view controller from the second. What you should really do is perform an unwind segue from the second and then immediately perform a segue to the third. The animations will be a bit undesirable by default since it will probably show the pop and the push, but to treat it like a tab bar, you could just disable the animations on the pop and push.
To implement, keep an int variable in the first view controller (btw.. you can do this by keeping the int in the navigation controller as well if you want). In the viewDidAppear, check the int variable. If it == 1 then push the second view controller. If it == 2, then push the third view controller. Else, don't do anything. When a button is pushed in the second view controller to view the third, unwind the second view controller, but in prepareForSegue set the int variable in the destination view controller to 2 . Thus, when the first view controller appears, it will immediately push the third view controller.
I know it is a bit annoying, but you don't really want to keep pushing the same view controllers over and over again without unwinding.