Skip to content
DeveloperMemos

Passing Data Through a Segue(iOS)

iOS, Segue, Data Passing1 min read

Passing data between view controllers is a common task in iOS app development. One way to accomplish this is by utilizing segues. Segues allow you to define the flow between different view controllers in your app and also facilitate the transfer of data. In this article, we'll cover various techniques for passing data through a segue in iOS using Swift.

Method 1: Using Properties

One straightforward approach to pass data between view controllers is by setting properties on the destination view controller. Here's an example:

1// Source View Controller
2class SourceViewController: UIViewController {
3 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
4 if let destinationVC = segue.destination as? DestinationViewController {
5 destinationVC.data = "Hello, World!"
6 }
7 }
8}
9
10// Destination View Controller
11class DestinationViewController: UIViewController {
12 var data: String?
13
14 override func viewDidLoad() {
15 super.viewDidLoad()
16 if let data = data {
17 print(data) // Output: Hello, World!
18 }
19 }
20}

In this example, we assign the value "Hello, World!" to the data property of the DestinationViewController during the segue's preparation phase. The destination view controller can then access this data in its viewDidLoad() method.

Method 2: Using Segue Identifier

Another way to pass data is by utilizing the identifier property of segues. This approach involves assigning a unique identifier to each segue in your storyboard and using it to differentiate between different segues. Here's an example:

1// Source View Controller
2class SourceViewController: UIViewController {
3 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
4 if segue.identifier == "DataSegue" {
5 if let destinationVC = segue.destination as? DestinationViewController {
6 destinationVC.data = "Hello, World!"
7 }
8 }
9 }
10}
11
12// Destination View Controller
13class DestinationViewController: UIViewController {
14 var data: String?
15
16 override func viewDidLoad() {
17 super.viewDidLoad()
18 if let data = data {
19 print(data) // Output: Hello, World!
20 }
21 }
22}

In this case, we assign the identifier "DataSegue" to the desired segue in the storyboard. During the preparation phase, we check for this identifier and pass the data accordingly.

Method 3: Using Delegates

Delegation is another powerful mechanism for passing data between view controllers. By defining a delegate protocol, the source view controller can communicate with the destination view controller and transfer data. Here's an example:

1// Destination View Controller
2protocol DataDelegate: AnyObject {
3 func sendData(_ data: String)
4}
5
6class DestinationViewController: UIViewController {
7 weak var delegate: DataDelegate?
8
9 override func viewDidLoad() {
10 super.viewDidLoad()
11 delegate?.sendData("Hello, World!")
12 }
13}
14
15// Source View Controller
16class SourceViewController: UIViewController, DataDelegate {
17 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
18 if let destinationVC = segue.destination as? DestinationViewController {
19 destinationVC.delegate = self
20 }
21 }
22
23 func sendData(_ data: String) {
24 print(data) // Output: Hello, World!
25 }
26}

In this example, the DestinationViewController defines a delegate protocol called DataDelegate. The source view controller (SourceViewController) adopts this protocol and becomes the delegate of the destination view controller. Through the delegate method sendData(_:), the data is transferred from the destination to the source.

Conclusion

Passing data between view controllers using segues is a fundamental aspect of iOS development. In this article, we explored three different methods for achieving this: using properties, segue identifiers, and delegates. Each approach has its advantages and may be suitable depending on the specific requirements of your app.