Sharing Data Between Controllers: Best Practices Swift — Part 1
By Naveen C — “Swift Savvy”
Data passing between controllers is a crucial aspect of application development, allowing information to be shared and utilized across different components. There are several methods available for accomplishing this, each with its advantages and use cases. Here are some common ways to pass data between controllers:
Let’s see the first component
A segue is passing data via a storyboard. It is the transition between two view controllers. It allows the user to move from one screen to another.
Segues have several transition styles.
In this, first ask the user to enter the name on the first screen and tap on the button to use the data later on the other screen.
Segues have several transition styles.
Here we going to pass the data between the controllers using segues.
Now Create a show segue between two view controllers via a storyboard. Click the UI element you are going to use for making the bridge and drag it to the second View Controller. Select the Show
option from the Action Segue
menu.
Now on the First View Controller, You need to override the prepare ( forsegue )
method.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var name = textField.text ?? ""
let destinationVC = segue.destination as! SecondViewController
destinationVC.name = name
}
Then your Second View Controller should be like this.
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var name: String = ""
override func viewDidLoad() {
super.viewDidLoad()
label.text = "Hi, " + name
}
}
An unwind segue is a special type of segue in iOS that allows you to navigate back to a previous view controller in your navigation stack. It is useful for implementing navigation flows where a user can move forward and backward through a series of view controllers.
Embed the view controller to the navigation stack, Here we use the same files but added a text field and button in the second view controller.
Now we are going to get the name from the user on the profile page( first Page ) and pass that name to the second page using segue as in the above example, when a user enters a password it will navigate to the previous page to check the password and displays the user’s name.
To use an unwind segue, you first need to create a method marked with the@IBAction
keyword with the parameter UIStoryboardSegue
in the view controller that you want to unwind to.
@IBAction func performUnwindSegueOperation(_ sender: UIStoryboardSegue) {
guard let VC = sender.source as? SecondViewController else { return }
if VC.textFieldSVC.text == "123" {
textField.isHidden = true
label.text = "Hi, " + VC.name
button.setImage(UIImage(systemName: "homekit"), for: .normal)
}
}
Here when we get back the data from the second view controller checking the password as 123
do some changes.
In the second view controller, we are just adding the necessary outlets for UI elements created.
Set the unwind segue action in the storyboard.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let name = textField.text ?? ""
let destinationVC = segue.destination as! SecondViewController
destinationVC.name = name
}
@IBAction func performUnwindSegueOperation(_ sender: UIStoryboardSegue)
{
guard let VC = sender.source as? SecondViewController else { return }
if VC.textFieldSVC.text == "123" {
textField.isHidden = true
label.text = "Hi, " + VC.name
button.setImage(UIImage(systemName: "homekit"), for: .normal)
}
}
}
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var textFieldSVC: UITextField!
var name: String = ""
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = name + " "
}
}
Segues are an essential part of iOS development that allows developers to easily navigate between view controllers in their apps. By defining segues in a storyboard, developers can create interactive user interfaces that guide users through their app’s functionality. We can pass data through the segues and it can be triggered by a variety of user actions, Such as button presses or table row selections , and can be customized with animations and other effects to enhance the user experience.
Thank you for reading this Article, Cheers to more readings…😻😻Here is the GitHub link for the complete project: Project Link
Stay tuned for the next component of our article, Coming soon to our blog!
Author
Reviewed by
Editor
We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com!
Leave a Reply