Storyboard Segue与Delegation:ViewController间传值选哪种方法更好?
Hey there! Totally get why you’re torn here—both Segues and Delegation are useful tools, and it’s super common for new iOS devs to wonder which one to reach for when passing data between ViewControllers. Let’s break this down clearly so you can pick the right approach for your scenario.
First off, you’re right—Segues make forward data passing (from a source ViewController to a destination one) dead simple. If your flow is something like "User taps a list item → open a detail ViewController and pass the selected item’s data," Segues are ideal here.
The beauty is that you get direct access to the destination VC in the prepare(for:sender:) method, so you can set its properties or call methods with just a few lines of code. For example:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Check if this is the segue we care about guard segue.identifier == "ShowDetailSegue", let detailVC = segue.destination as? DetailViewController else { return } // Pass data to the destination VC detailVC.selectedItem = mySelectedData }
Use Segues when:
- You’re passing data from a parent to a child VC (forward direction)
- Your navigation flow is defined in Storyboards (you’ve already set up the Segue between VCs)
- The data transfer logic is straightforward and one-way
Delegation isn’t really a competitor to Segues—it solves a different problem. Where Segues handle forward passes, Delegation is designed for reverse data flow (from child back to parent) or letting a child VC notify its parent about events (like a "save" button tap, or a user completing a form).
It’s a core iOS design pattern because it keeps your ViewControllers loosely coupled—your child VC doesn’t need to know exactly what the parent will do with the data, just that it will follow a defined protocol. Here’s a quick example:
First, define a protocol in your child VC:
protocol ChildVCDelegate: AnyObject { func childViewControllerDidSubmit(data: String) } class ChildViewController: UIViewController { // Weak reference to avoid retain cycles weak var delegate: ChildVCDelegate? @IBAction func submitButtonTapped(_ sender: UIButton) { // Notify the parent VC when the user submits delegate?.childViewControllerDidSubmit(data: "User input from child") dismiss(animated: true) } }
Then have your parent VC conform to the protocol and set itself as the delegate:
class ParentViewController: UIViewController, ChildVCDelegate { override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard let childVC = segue.destination as? ChildViewController else { return } // Set the delegate so the child can talk back childVC.delegate = self } // Implement the delegate method to receive data from the child func childViewControllerDidSubmit(data: String) { print("Received data from child: \(data)") // Do something with the data (update UI, save to Core Data, etc.) } }
Use Delegation when:
- You need to pass data from a child back to its parent VC (reverse direction)
- A child VC needs to trigger an action in the parent (like updating the UI or saving data)
- You want to keep your VCs decoupled (so changes to one don’t break the other)
- Segues alone: Simple one-way forward data passing (e.g., list → detail)
- Delegation alone: Reverse communication or event notifications (e.g., modal form → parent VC)
- Both together: Most common! Use Segues to pass initial data to a child VC, then Delegation to send results back to the parent.
Don’t stress too much about picking the "right" one right away. Start with Segues for simple forward passes, then experiment with Delegation when you need to send data back or keep your VCs loosely coupled. You’ll get the hang of it in no time!
内容的提问来源于stack exchange,提问作者Tigran

