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.
Passing data to destination in 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.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showProfile" { let profileVC = segue.destination as! ProfileViewController profileVC.username = "Alice" } }
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 } } }
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.
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) } }
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.
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.
Practice
Solution
Step 1: Understand data sharing between screens
Passing data allows one screen to send information to another so the next screen can show content based on that data.Step 2: Identify the purpose of passing data
This process is mainly to make the user experience relevant by showing specific information on the destination screen.Final Answer:
To share information so the next screen can display relevant content -> Option DQuick Check:
Passing data = share info for relevant display [OK]
- Confusing data passing with UI styling
- Thinking it improves performance directly
- Assuming it handles authentication
Solution
Step 1: Identify the method for segue preparation
The method prepare(for:sender:) is called before a segue happens and is used to set data on the destination controller.Step 2: Confirm other methods are unrelated
viewDidLoad() and viewWillAppear() are lifecycle methods but not used for passing data during segue. applicationDidBecomeActive() is an app lifecycle method.Final Answer:
prepare(for:sender:) -> Option AQuick Check:
Data passing before segue = prepare(for:sender:) [OK]
- Using viewDidLoad() to pass data
- Confusing lifecycle methods with segue methods
- Trying to pass data after segue completes
destinationVC.userName after the segue?override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
let destinationVC = segue.destination as! DetailViewController
destinationVC.userName = "Alice"
}
}Solution
Step 1: Check the segue identifier and casting
The code checks if the segue identifier is "showDetail" and casts the destination to DetailViewController safely using forced cast.Step 2: Assign the userName property
It sets destinationVC.userName to "Alice" before the segue completes.Final Answer:
"Alice" -> Option AQuick Check:
prepare sets userName = "Alice" [OK]
- Forgetting to check segue.identifier
- Not casting destinationVC correctly
- Assuming userName remains nil
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! DetailViewController
destinationVC.data = "Hello"
}Solution
Step 1: Identify missing segue identifier check
The code casts destinationVC without checking if the segue.identifier matches the expected one, risking wrong destination type.Step 2: Understand crash cause
If the segue is not to DetailViewController, forced cast fails and crashes the app.Final Answer:
Missing check for segue.identifier before casting -> Option CQuick Check:
Always check segue.identifier before casting [OK]
- Skipping identifier check causes crash
- Assuming forced cast is always safe
- Ignoring optional cast or guard statements
GameViewController to ResultViewController using a segue with identifier "showResult". Which code snippet correctly passes the score safely?Solution
Step 1: Check segue identifier and safely cast destination
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showResult" { guard let dest = segue.destination as? ResultViewController else { return } dest.score = userScore } } uses a guard let to safely cast segue.destination to ResultViewController only if the identifier matches "showResult".Step 2: Assign the score property safely
Inside the guard, dest.score is assigned userScore safely, avoiding crashes if cast fails.Final Answer:
Checks identifier and uses guard let for safe casting -> Option BQuick Check:
Safe cast + identifier check = override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showResult" { guard let dest = segue.destination as? ResultViewController else { return } dest.score = userScore } } [OK]
- Forcing cast without identifier check (Option A)
- Not checking identifier before casting (Option B)
- Not casting destination at all (Option D)
Ready to prove it?
Pass the test and this pattern moves unknown
