I am little confused with the way that segue is working in my app.
I have TabBarController and my ViewController inside it is embedded in NavigationController.
I added identifier to segue "mySegue". So when i perform some action inside my ViewController I run
performSegue(withIdentifier: "mySegue", sender: self)
So segue is performed but I have some things that I don't understand.
View is presented without tab bar and navigation bar. Why? I thought that they should be visible by default.
I want segue to be animated - so view will be presented from right to left and also I will be able to unwind it (simply go back by swiping). But swipe back is not working and view is presented from bottom to top. Why is it?
You should set the segue's type to Push - that way the navigagtion controller will issue the hierarchy and it will actually get pushed to its stack, hence the name.
If you want your segue to transits horizontally, you need to use (Show e.g push) for the segue, like this. It has swipe back by default
As it was not the first time when I meet this problem - I need to point where the problem was. Maybe it will help others when it seems that everything in segue is made right way but it is not working the way it should.
In my case - it was because segue was created in interface builder by dragging not from the ViewController's yellow circle to second ViewController. Segue was created from one of subviews or even TableView / TableViewCell inside ViewController. So in that case it wasn't creating segue in NavigationController.
Related
I am performing a segue to open another ViewController, afterwards I go back to the initial ViewController also via performSegue.
If I am doing this, all the values in the initial ViewController (e.g. the image in the UIImageView) are destroyed.
Do I need to save everything before I perform a Segue, or am I doing something completely wrong?
Thanks!
EDIT:
This is what I would like to achieve, if I press the menu button on my "HomeVC", the tableView of the SWRevealViewController pops out from the left side. If I press a cell, I get with segue push, to another UINavigationController. From this Controller I would like to go back to the "HomeVC".
Here is a screenshot.
You do it wrong. Performing a segue will create a brand new controller from the segue's destination and present/show it.
If you want to go back, you shouldn't perform another segue. Instead, you must dismiss() it if you presented the newController modally or pop() it if you show/push it.
This tutorial may help
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'm trying to connect two View Controllers in my storyboard with segues. Usually I just had to control drag and then connect them and select the segue I wanted. However now I just control drag, then the blue line appears however when I drop it nothing happens. No pop up, no connection. What happened?
Should all work the same. Be sure to drag from your view controller to the the view you'll transition to. Give the segue an identifier string and then you can transition with performSegueWithIdentifier.
self.performSegueWithIdentifier("segueIdentifier", sender:self)
I basically want to "get rid" of the NavigationController I have set up for my login/registration NavigationController after the login/registration has been successful. Basically, I am doing a manual segue after executing some code that runs through an IBAction when the Login/Register button is pressed. My code runs just fine, but the thing is that whenever the segue executes, my second view controller, a TabBarController (which I want to somewhat make independent of the NavigationController) still has the navigation bar on top of the view (with the <Back button). I am doing the transition through a push segue, which kind of makes me realize that it is not the correct approach since it means I am nesting the TabBarController into the NavigationController itself. It's just that I do not know how to make it independent.
My approach is the following:
//...After various checks/functions, inside a function, I redirect myself to the TabBarController
self.performSegueWithIdentifier("redirectToMenuFromRegistration", sender: nil)
//Where the segue "redirectToMenuFromRegistration" is my TabBarController..
As I said, I do not want my TabBarController to be nested within my NavigationController, I want it to be somehow independent. Now another question. If I do this, will I still be able to pass data through ViewControllers?
Thank you so much for your help!
Cheers!
Set segue "Show" to "Present Modally" in storyboard.
Custom transitions are fairly new to me. I had to incorporate them into the last project I worked on and ended up using both the UIViewControllerAnimatedTransitioning protocol and custom segues.
Custom segues seem a bit cleaner/friendly in that you can choose them in a storyboard and be done with it. However there doesn't seem to be as-friendly way to set up a back/pop segue. I've read about unwinding segues but I can't seem to find anything around tying one to the back button of a nav controller.
The UIViewControllerAnimatedTransitioning protocol approach has a bit more set up but allows you to specify both the entering and exit of the views.
In the case of my app not being able to pop back with my custom segues wasn't an issue because the flow of the app doesn't allow you to go back to the previous views. Most applications though require this thus a custom segue seems worthless unless you subclass UINavigationController and allow for custom popping segues.
Am I missing something because it seems UIViewControllerAnimatedTransitioning is the best approach to take for animations between view controllers. Why would I ever want to subclass UIStoryboardSegue?
I agree that there is a lack of documentation on unwind segues compared to other types of segues, but after you get the hang of them, they are pretty straightforward. My understanding is that segues (including unwind segues) is the way that Apple intends you to transition between view controllers. Even when you create custom segues, the regular unwind segues should still function.
In my own work, I have subclassed a UIStoryboardSegue to execute a custom animation when transitioning between segues. Using a widely known app as an example, when you tap the menu button in Uber's app, the map view controller moves down, and a table view controller appears. And when you a tap a row in the table, the new view controller slides in from the right, but the map view controller is still visible on the screen. And when you tap the map view controller, it returns to its original position. For some reason, I believe that Uber actually didn't implement segues at all, but just place view controllers on top of view controllers, but I have implemented something similar in my own app with custom segues. These segues are difficult to replicate with Apple's default segues, so I used custom ones.
If you are tying an unwind segue to a back button, then you will want to override the normal unwind that comes along with the default back button in the uinavigationcontroller. I have found it very difficult to customize that segue. I would recommend hiding the default back button that comes with the uinavigationcontroller, adding your own bar button, and tying this new button to an unwind segue. I know that this is annoying, considering the default back button has some added functionality, such as using the title from the previous view controller as its text when the title is short enough. Unfortunately though, I think Apple really wants to discourage you from customizing the default button and makes it difficult to alter. I have left out how to replace the default back button with the custom one, so let me know if you have trouble.
Anyway, to create the unwind segue, you must first create a method in the class (or parent class) of the uiviewcontroller you are unwinding to (not from).
- (IBAction) methodName:(UIStoryboardSegue *)segueName
You can change the methodName and segueName to whatever you like. Now go to your storyboard, and go to the scene containing the uiviewcontroller you are unwinding from. If you now ctrl drag from this uiviewcontroller (the yellow button on the left side of the bar at the top of the controller) to the exit button (the orange button on the right side of the same bar) you will now see a menu popping up that contains the methodName above. Click that method, and your unwind segue will now be created.
After you have created the unwind segue, you now see it in the outline on the left of the storyboard. If you click the segue, you can inspect it and give it an identifier.
Now to deal with tying it to the back button...
Again, viewing the uiviewcontroller you are unwinding from , if you control drag from the custom back button (not the default back button) in the storyboard to the class of this uiviewcontroller, an IBAction will be created for you tied to the button. In this method, add in:
[self performSegueWithIdentifier: segueIdentifier sender:nil];
where segueIdentiferis the identifier you gave the unwind segue above. Now when you tap the back button, the unwind segue will be executed. You can also do some animations or what not before the unwind segue is executed.
I have actually done some complicated custom unwind segues dealing with animating both the source and destination view controllers. If you can be more specific regarding how you would like the unwinding to look, I can try to help you out.