How To Use A Segue In Swift
You’ve just started your first project as an iOS Developer, no doubt you’ve create a beautiful UIStoryboard. You’ve probably even created some sort of menu that you want to take you from one place to another once tapped, the problem now is, how exactly do I get from one storyboard to another? The solution is a segue!
Lets Start With The Swift Documentation
UIStoryboardSegueclass supports the standard visual transitions available in UIKit. You can also subclass to define custom transitions between the view controllers in your storyboard file. Segue objects contain information about the view controllers involved in a transition. When a segue is triggered, but before the visual transition occurs, the storyboard runtime calls the current view controller’s
prepare(for:sender:)method so that it can pass any needed data to the view controller that is about to be displayed.
The Easiest Solution To Segue From Controller To Controller
Workable? Yes. The Solution You’re Looking For? Likely Not
That might be the easiest solution, and involve no code, but it gives very little flexibility. For instance, what if you want that button to transition to different places depending on certain circumstances? For instance, in an app I’m working on, I have quizzes, and if you tap on a quiz that hasn’t been purchased yet, instead of transitioning to that quizController, you transition to a viewController that displays information about that quiz and how to purchase it. Transitioning through code is a much more flexible solution, but it requires a few things.
What You’ll Need To Transition From One View To Another
- A segue. Instead of creating this segue from control dragging directly from a button, control drag from the viewController in the scene inspector to your desired controller. The scene inspector is the bar at the top of your controller, which has three icons, the viewController, the first responder, and the exit.
- An identifier. Your segue has to have an identifier, so that swift knows which segue is supposed to be triggering in the case that you have have multiple segues, just like in the example below. Without an identifier, when you pressed your button, Xcode wouldn’t know whether to send you to controller B, or controller C, which would likely either not get you where you want to be, or even worse, result in a crash.
You can set the identifier by clicking directly on the segue, and setting it in the identity inspector. Make sure to give this a descriptive name, and remember it, or at least where to find it, because if you don’t reference this name down to the letter when you use the performSegue method, you’ll get a crash. I’ll show you a way to avoid this later.
Thats it guys, that’s the small amount code you need to perform segue. The identifier will be the one you set in the previous step, that way Xcode will always know which segue you’re referring to. While it may seem easy to transition from one controller to another using segues, that it not their only primary function. You will need segues to pass data from one controller to another, and that is what the sender is for. However, these are bite sized tutorials, and if I went on to explaining passing data using segues, this would go from bite sized to a mouthful very quickly. That will be saved for another tutorial.
Remember when I mentioned that tip earlier to make getting the right identifier easier? This is it. You want to avoid typing things in manually as much as possible to prevent bugs, so one solution would be to create a constant and store your identifier that way, so that all you need to do is start typing your constant, and always have peace of mind that it is correct. Another advantage is that instead of having to find every instance you’ve used it, just change the one constant!
If you’ve been following along, you’ve probably noticed that yay, you’ve create a segue and have successfully transitioned to another view! But now that you’re here, you don’t exactly have a way to get back! The bite sized way is to embed your controller in a navigationController! This will give you your back button by default, among many other things!
Now that you’ve seen how to give yourself a back button using a UINavigationController, try customizing the UINavigationController to give it something other than the ugly, default settings that likely don’t fit in at all with the beautiful UI you’ve no doubt created!