UIViewControllerAnimatedTransitioning vs custom UIStoryboardSegue - ios

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.

Related

Segue misunderstanding

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.

For swift iOS app dev, how do I segue to a view that takes up part of the screen only?

GroupMe is a good example. I want to implement something similar to this..
when you press the top left icon it'll bring you to the view on the left hand side, and when you click the right hand side of that view (the chats) it will bring you back
As the documentation says:
Use segues to define the flow of your app’s interface. A segue defines
a transition between two view controllers in your app’s storyboard
file. The starting point of a segue is the button, table row, or
gesture recognizer that initiates the segue. The end point of a segue
is the view controller you want to display. A segue always presents a
new view controller, but you can also use an unwind segue to dismiss a
view controller.
As said by Apple© segues are for changing View Controllers only and cannot be used to move single views. I suggest using animations to achieve the effect you are searching, however segues are definitely not what you are looking for. Maybe this question on stackoverflow.com may help you.
And please be sure to check for information by yourself first, I got this information in less than 5 minutes by searching on Google.com.
Regards -Jorge

Unwind segue doesn't work

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)?

Xcode / Swift: How I implement a back 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.

Unwind segue function with Back button

I'm trying to call function when i'm pressing default Back button (e.g. < Settings) in Navigation Controller.
At other situations i'm creating custom button, after i'm Ctrl+Dragging to red Exit button in XCode and pointing it to Selection segue.
Now i need to use default back button of navigation controller, but i can't set assign it to segue action because it's no back button in Storyboard view (but it exists in Simulator).
Simulator:
XCode:
I can create custom button in navigation contoller and assign it so custom unwind segue, but that button will be ugly without "< " symbol.
I've found some answers at stackoverflow, but they all are written in Objective-C :(.
You can use a delegate method to accomplish what you are doing.
Here is a really thought out swift tutorial that deal with delegates and segues:
http://makeapppie.com/2014/07/05/using-delegates-and-segues-part-2-the-pizza-demo-app/
What you are asking, ability to catch the back button's action, isn't possible the way you describe it.
Most people would recommend creating a custom button, then use an unwind segue.
There are lots of tutorials on custom back buttons just in case you're new to swift, and the ugly part of the custom back button can be easily fixed by putting your own arrow there.
Also I found this previous asked question for future reference: Unwind segue from navigation back button in Swift
Actually I just assigned the unwind segue to the entire view controller (right click from the yellow view controller outlet and drag to the exit outlet then select the function). And it works great so far. No need custom button, no delegate.
In fact, in my project the child is supposed to call the different parent's function according to current caller view controller and simply using the same function name made my dreams come true.
This page helped me a lot: http://spin.atomicobject.com/2014/12/01/program-ios-unwind-segue/

Resources