← Back to list

How to Use Multiple Segues to Connect to the Same Page?

📕📗📙 100 Questions and Answers — Question 10

Chiau's Tech Insight Lab · 2025-01-15 08:47 · 13 claps · 2.0 min read paywalled
#segue #pass-data #transfers #ibsegueaction #swift-programming
Open on Medium ↗
Wiki topics: 💻 · Programming 📱 · Mobile Development

How to Use Multiple Segues to Connect to the Same Page?

📕📗📙 100 Questions and Answers — Question 10

In Swift, to transfer data to the next page, we typically use a segue to connect the two pages. For example, if we have three options on the first page, we need to connect them to three different pages separately, as shown below.

However, imagine if we have one hundred buttons. Should we have one hundred corresponding pages? One common way to solve this problem is to connect multiple segues to one page. In this way, we can use only two controllers to achieve the desired effect, as shown below.

In Swift, we use identifiers to distinguish between different segues and display corresponding content on the next page based on the segue. An identifier for a segue can be set up in its properties, as shown in the screenshot below.

In this example, we use two segues: we named the blue segue ‘segue1’ and set the pink segue to ‘segue2.’ Then, in the IBSegueAction function, a switch statement can be used to check the triggered segueIdentifier.

@IBSegueAction func show(_ coder: NSCoder, sender: Any?, segueIdentifier: String?) -> iPhone1ViewController? {

        let controller = iPhone1ViewController(coder: coder)

        switch segueIdentifier{
        case "segue1":
            controller?.iPhoneInfo=iPhone(imageName: "iPhone1", iphoneName: "Blue", Description: "Cool Blue")
        case "segue2":
            controller?.iPhoneInfo=iPhone(imageName: "iPhone2", iphoneName: "Pink", Description: "Pretty Pink")
        default:
            break
        }
        return controller
  }

To pass data to the next page, we declare a structure called iPhone, which includes three properties.

struct iPhone{
    let imageName:String
    let iphoneName:String
    let Description:String
}

In this way, we can pass the corresponding data to the next page based on the triggered button. Then, on the next page, we only need to display the information passed from the first page. The entire code is shown below.

 @IBOutlet var imgView: UIImageView!
 @IBOutlet var imgName: UILabel!
 @IBOutlet var imgDescription: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        imgView.image = UIImage(named: iPhoneInfo.imageName)
        imgName.text = "\(iPhoneInfo.iphoneName)"
        imgDescription.text = "\(iPhoneInfo.Description)"
  }

If you have any questions or suggestions, feel free to comment below.


메타데이터
post_id
fea0d1ff7002
slug
how-to-use-multiple-segues-to-connect-to-the-same-page-fea0d1ff7002
url
https://medium.com/@chiauli/how-to-use-multiple-segues-to-connect-to-the-same-page-fea0d1ff7002
canonical_url
https://medium.com/@chiauli/how-to-use-multiple-segues-to-connect-to-the-same-page-fea0d1ff7002
author_url
https://medium.com/@chiauli
status
ok
fetched_at
2026-07-21 07:49:06