0
0
iOS Swiftmobile~3 mins

Why Passing data to destination in iOS Swift? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could send information between screens without writing messy code every time?

The Scenario

Imagine you have two screens in your app: one shows a list of friends, and the other shows details about a selected friend. You want to send the friend's name and photo from the list screen to the detail screen manually.

The Problem

Doing this manually means writing lots of code to find the right screen, create it, and then copy each piece of data one by one. It's easy to forget something or make mistakes, and the code becomes messy and hard to fix.

The Solution

Passing data to destination lets you send all the needed information smoothly when moving from one screen to another. It keeps your code clean and makes sure the right data arrives exactly where it should.

Before vs After
Before
let detailVC = DetailViewController()
detailVC.name = selectedName
detailVC.photo = selectedPhoto
navigationController?.pushViewController(detailVC, animated: true)
After
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if let detailVC = segue.destination as? DetailViewController {
    detailVC.name = selectedName
    detailVC.photo = selectedPhoto
  }
}
What It Enables

This makes your app flow naturally, letting users see the right information instantly when they move between screens.

Real Life Example

When you tap a contact in your phone's address book, the app passes that contact's details to the next screen so you can see their phone number and email without delay.

Key Takeaways

Passing data to destination avoids messy manual copying.

It keeps your app's navigation smooth and reliable.

It helps users get the right info exactly when they need it.