What is the common way to pass data between view controllers in iOS using Swift?
You usually pass data by setting a property on the destination view controller before the transition happens, often inside the prepare(for:sender:) method.
Click to reveal answer
beginner
What method do you override to prepare data before a segue transition?
Override the prepare(for segue: UIStoryboardSegue, sender: Any?) method to access the destination view controller and pass data to it.
Click to reveal answer
intermediate
How do you safely get the destination view controller inside prepare(for:sender:)?
Use optional binding (if let) to cast segue.destination to the expected view controller type before setting its properties.
Click to reveal answer
intermediate
Why is it important to pass data before the segue transition completes?
Because the destination view controller's view loads after the segue, so setting data beforehand ensures it is ready when the new screen appears.
Click to reveal answer
advanced
Can you pass data back from the destination to the source view controller using prepare(for:sender:)?
No, prepare(for:sender:) is only for passing data forward. To pass data back, use delegation, closures, or unwind segues.
Click to reveal answer
Which method is used to pass data to the next view controller during a segue?
Aprepare(for:sender:)
BviewDidLoad()
CapplicationDidBecomeActive()
DdidReceiveMemoryWarning()
✗ Incorrect
prepare(for:sender:) is called before the segue transition and is the place to pass data to the destination view controller.
How do you access the destination view controller inside prepare(for:sender:)?
Asegue.source
Bsegue.destination
Cself.navigationController
Dself.presentedViewController
✗ Incorrect
segue.destination gives you the view controller you are navigating to, so you can set its properties.
What Swift feature helps safely cast the destination view controller to the correct type?
AImplicitly unwrapped optionals
BForced unwrapping (!)
COptional binding (if let)
DType inference
✗ Incorrect
Optional binding (if let) safely unwraps and casts the destination view controller to the expected type.
When should you pass data to the destination view controller?
AInside viewDidDisappear() of source
BInside viewDidAppear() of destination
CAfter the segue completes
DInside prepare(for:sender:) before segue
✗ Incorrect
Passing data inside prepare(for:sender:) ensures the destination has the data before its view loads.
Which method is NOT used to pass data back from a destination view controller?
Aprepare(for:sender:)
BDelegation
CUnwind segues
DClosures
✗ Incorrect
prepare(for:sender:) is only for passing data forward, not back.
Explain how to pass data from one view controller to another using a segue in Swift.
Think about the method called before the screen changes.
You got /3 concepts.
Describe ways to send data back from a destination view controller to the source view controller.
Passing data back is different from passing data forward.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of passing data to a destination view controller in iOS Swift?
easy
A. To change the app's theme colors dynamically
B. To handle user authentication automatically
C. To improve app performance by caching data
D. To share information so the next screen can display relevant content
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 D
Quick Check:
Passing data = share info for relevant display [OK]
Hint: Passing data means sharing info for next screen display [OK]
Common Mistakes:
Confusing data passing with UI styling
Thinking it improves performance directly
Assuming it handles authentication
2. Which method is commonly overridden to pass data to the destination view controller before a segue?
easy
A. prepare(for:sender:)
B. applicationDidBecomeActive()
C. viewWillAppear()
D. viewDidLoad()
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 A
Quick Check:
Data passing before segue = prepare(for:sender:) [OK]
Hint: Use prepare(for:sender:) to pass data before segue [OK]
Common Mistakes:
Using viewDidLoad() to pass data
Confusing lifecycle methods with segue methods
Trying to pass data after segue completes
3. Given the code snippet below, what will be the value of destinationVC.userName after the segue?
C. Missing check for segue.identifier before casting
D. Not calling super.prepare(for:sender:)
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 C
Quick Check:
Always check segue.identifier before casting [OK]
Hint: Always check segue.identifier before casting destination [OK]
Common Mistakes:
Skipping identifier check causes crash
Assuming forced cast is always safe
Ignoring optional cast or guard statements
5. You want to pass a user's score (an Int) from GameViewController to ResultViewController using a segue with identifier "showResult". Which code snippet correctly passes the score safely?
hard
A. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showResult" {
let dest = segue.destination as! ResultViewController
dest.score = userScore
}
}
B. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showResult" {
guard let dest = segue.destination as? ResultViewController else { return }
dest.score = userScore
}
}
C. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let dest = segue.destination as? ResultViewController
dest?.score = userScore
}
D. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showResult" {
let dest = segue.destination
dest.score = userScore
}
}
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 B