banner image 1 banner image 2

Sharing Data Between Controllers: Best Practices Swift — Part 1

May 17, 2023
5 mins
command
blog-img 1
Naveen C
Author

This article will help you to understand the different ways of data-passing techniques in Swift iOS.

By Naveen C — “Swift Savvy”


Sharing Data Between Controllers: Best Practices Swift — Part 1

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:

  1. Segues
  2. Delegate design pattern
  3. Singleton design pattern
  4. Closures
  5. Notification Center

Let’s see the first component

1. Segues

Segues

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.

  1. Show Segue: The default segue is Show Segue, which is used to show a new view controller and present it on top of the current view controller.
  2. Show Detail Segue: This type of segue is similar to the Show Segue, but it is used for split view controllers. The new view controller will appear on the right side and the old view controller would be on the left side.
  3. Present Modally Segue: This type of segue presents a new view controller modally on top of the previous view controller. The new view controller covers the entire screen and is typically used for showing a form or alert.
  4. Popover Presentation Segue: This type of segue displays a popover view controller on top of the current view controller. This is typically used for displaying additional information or options.
  5. Unwind segue: This type of segue allows you to return to a previous view controller. It is typically used for canceling or saving changes made in a new view controller.
  6. Custom segue: This type of segue allows you to create a custom transition between two view controllers. You can define your own animation and transition behavior using code.

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 Showoption 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

}

}

Now we are going to return data to the parent view controller using unwind segue

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 123do 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.

ViewController.Swift

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)
}
}

}

SecondViewController.Swift

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!


References :

[embed]https://developer.apple.com/documentation/uikit/uistoryboardsegue/[/embed][embed]https://developer.apple.com/documentation/uikit/uistoryboardsegue/[/embed]

Meet The Team!!

Author

Naveen C

Reviewed by

Ulaganathan Pv

Editor

Seema Jain


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!
blog-img 2

Discussions

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P

blog-img 3
7 mins
March 17, 2023
Exploring Layout Managers in Android: Constraint, ...

This article will give you a fair understanding of

By Aishwarya S