0
0
iOS Swiftmobile~5 mins

Passing data to destination in iOS Swift

Choose your learning style9 modes available
Introduction

Passing data to destination means sending information from one screen to another in your app. This helps screens show the right details based on what the user did before.

When you tap on a list item and want to show details about that item on a new screen.
When you fill a form and want to send the entered data to the next screen for confirmation.
When you select a setting and want the next screen to know which option was chosen.
When you want to pass user preferences from one screen to another to customize the display.
Syntax
iOS Swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "showDetail" {
    let destinationVC = segue.destination as! DetailViewController
    destinationVC.data = someData
  }
}

This method runs before the screen changes, so you can set data on the new screen.

Make sure to set the segue identifier in your storyboard to match the code.

Examples
Passing a username string to the Profile screen before it appears.
iOS Swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "showProfile" {
    let profileVC = segue.destination as! ProfileViewController
    profileVC.username = "Alice"
  }
}
Passing a selected item object to the Item screen using the sender parameter.
iOS Swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "showItem" {
    let itemVC = segue.destination as! ItemViewController
    if let selectedItem = sender as? Item {
      itemVC.item = selectedItem
    }
  }
}
Sample App

This example shows a Master screen that passes a color name to a Detail screen. When the button is tapped, the app moves to the Detail screen and shows the selected color.

iOS Swift
import UIKit

class MasterViewController: UIViewController {
  var selectedColor: String = "Blue"

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showColorDetail" {
      let detailVC = segue.destination as! DetailViewController
      detailVC.colorName = selectedColor
    }
  }

  @IBAction func showDetailTapped(_ sender: UIButton) {
    performSegue(withIdentifier: "showColorDetail", sender: nil)
  }
}

class DetailViewController: UIViewController {
  var colorName: String?

  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    let label = UILabel()
    label.text = "Selected color: \(colorName ?? \"None\")"
    label.textAlignment = .center
    label.frame = view.bounds
    view.addSubview(label)
  }
}
OutputSuccess
Important Notes

Always check the segue identifier to avoid passing data to the wrong screen.

Use optional binding to safely unwrap data when needed.

Remember to set the destination screen's properties before the transition happens.

Summary

Passing data lets screens share information to show relevant content.

Use prepare(for:sender:) to set data on the destination screen before navigation.

Check segue identifiers and safely unwrap data to avoid crashes.