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”.
@IBAction func goButtonClicked(_ sender: Any) { performSegue(withIdentifier: "goToSecondView", sender: self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "goToSecondView"{ let secondView = segue.destination secondView.modalPresentationStyle = .fullScreen } }
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.
import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func goButtonClicked(_ sender: Any) { performSegue(withIdentifier: "goToSecondView", sender: self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "goToSecondView"{ let secondView = segue.destination as! SecondViewController secondView.modalPresentationStyle = .fullScreen secondView.data = textField.text! } } }
import UIKit class SecondViewController: UIViewController { var data = "" @IBOutlet weak var label: UILabel! override func viewDidLoad() { super.viewDidLoad() label.text = data // Do any additional setup after loading the view. } @IBAction func goBackButtonClicked(_ sender: Any) { dismiss(animated: true, completion: nil) } }
For your questions you can reach me via e-mail or comments.