Bird
0
0

You need to send an array of strings from FirstViewController to SecondViewController before navigation. Which approach correctly accomplishes this in Swift?

hard📝 Application Q8 of 15
iOS Swift - Navigation
You need to send an array of strings from FirstViewController to SecondViewController before navigation. Which approach correctly accomplishes this in Swift?
ACreate a new instance of SecondViewController, assign the array, then call present(_:animated:)
BIn prepare(for:sender:), cast segue.destination to SecondViewController and assign the array to a property declared in SecondViewController
CStore the array in UserDefaults and retrieve it in SecondViewController's viewDidLoad()
DPass the array as a parameter in the segue's identifier string
Step-by-Step Solution
Solution:
  1. Step 1: Use prepare(for:sender:)

    This method is designed to pass data before segue transitions.
  2. Step 2: Cast destination VC

    Cast segue.destination to SecondViewController to access its properties.
  3. Step 3: Assign array property

    Assign the array to a property declared in SecondViewController for use after transition.
  4. Final Answer:

    In prepare(for:sender:), cast segue.destination to SecondViewController and assign the array to a property declared in SecondViewController correctly uses the segue mechanism and property assignment.
  5. Quick Check:

    Use prepare and cast destination VC [OK]
Quick Trick: Assign data in prepare(for:sender:) after casting [OK]
Common Mistakes:
  • Creating new VC instance instead of using segue.destination
  • Using UserDefaults for temporary data passing
  • Trying to encode data in segue identifier

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes