Press "Enter" to skip to content

Swift Segue

Hello, when we develop an iOS application using with the Swift,we may want to routing between the pages. To route between the pages in iOS, we need the use Segue. Segue help us the navigate between the View Controllers.

To show how to use segue, I created a Blank Swift project in Xcode. In the Main.storyboard, I added a new ViewController and i created a new class which name is SecondViewController and then i connected the new ViewController to the SecondViewController.

Now, I changed the background color of the ViewControllers and I added the a label, a textfield and a button to each ViewController. In the first ViewController, I press the button and command(cmd) and drag the second view controller. After that, some options appear and i chose the Show. When we run the app and click the button, Routing will be happen between the pages. It is important to give a identifier to the segue.

When the app routing, page can be appear like popover. To solve that we need write some codes. Firstly, I made the different routing and the page will be appear full screen. Now, I deleted the past segue and I created a new routing from the top of the view controller. I clicked the circle image and pressed the control then drag the other ViewController. I gave the identifier name “goToSecondView”. After that, Go to the view controller and override the prepare function and i gave the identifier name. In the function, I created a view from the destination and i gave the full screen property. Finally, I made the @IBAction and add the performsegue with the identifier name “goToSecondView”.

Now, We learned the how to route between the pages. I would like to send data while routing in the app. For that, I go to the SecondViewController and i created a empty string which name is data. And then, I gave the view as a SecondViewController and define the data from the textfield text. When we run the app, We can see that the data sent. Finally, I want to go back when i clicked the go back button. For that, I created the @IBAction for the button and i wrote the dismiss function so we can go back when we clicked the button.

For your questions you can reach me via e-mail or comments.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *