The first segue

We will learn how to create segues using the storyboard and how to trigger them using the code. Each segue is a relation (transition) between different screens in your app, and those transitions can be fired upon a user's action or by using any other trigger (time trigger, or an action from a server):

  1. The easiest way to create a segue is to hold Ctrl and then drag it from the view controller or button to the view controller, which should be presented on the screen. In our case, we can start with the home screen. Let's start a Ctrl drag from the Favorites button and then drop it on the next view controller (the favorites). The following screenshot shows what this should look like:
  1. Once you lift the mouse, a small popup is displayed, asking you to pick the action to be used. This action will define how the new view controller will be presented on the screen. We will use the Push (deprecated) option, but in other cases, some other options are better:
  1. Once we pick an action, a connection between our two view controllers will be added on the storyboard:

This link represents a relation between those two screens. If we add another segue, which will lead to another view controller, then we will have another link between these view controllers.

You can select the segue and add an identifier in the properties panel on the right. This identifier can be used to trigger a segue with code. For example, the selected segue has the following ID—showFavorites:

The segue that we created is tightly coupled with the button and it will be activated when the button is touched:

Another way to activate a segue is to use code and then add an action that will be fired upon using the Touch Up Inside event. Then in the function, you can activate the segue with the following code:

         @IBAction func onFavoritesClicked(_ sender: Any) {
performSegue(withIdentifier: "showFavoritesAlternative",
sender: sender)
}

You should create a segue with the showFavoritesAlternative identifier on the storyboard. A generic segue, which will be started from the code, can be defined with a Ctrl drag from a view controller to the next view controller:

This is an example in which we link the next two view controllers, but you can define as many segues as you need. They will be displayed on the storyboard and this will help you visualize the complex flows in your applications.

Now, we know how to transition from one scene to another, but we should pass data and make the user believe that those scenes are related. We define each screen as a template, which could present specific information. We have to pass that information, and the view controller will handle it from there.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.244.217