user00007
user00007

Reputation: 93

How to add multiple segues from one viewController

I have a UI view controller(viewControllerOne) with a table view in it. I have a segue from a button in the cell to another viewController (viewControllerTwo).

This works fine.
now , i have a third view controller and want to have a segue from another button in View Controller one . How to have multiple segues from one view controller to go to different viewController.

Upvotes: 2

Views: 2593

Answers (2)

KingCoder11
KingCoder11

Reputation: 416

Segues inside UITableViewCell should be done programmatically. Use the didSelectCell at indexPath method. When this is called, you should perform a segue based on the indexPath. I personally recommend having an array of structs to keep track of the UITableViewCells. For example:

struct tableViewCell {
    var cell: UITableViewCell
    var segue: String
}

var cells: Array<Array<tableViewCell>> = [];

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
    self.performSegue(withIdentifier: cells[indexPath.section][indexPath.row].segue);
}

Sorry if there are any typos, this code was not written in xcode.

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

First segue should be between the VC itself ( not a button inside the cell ) to the destinationVC as to be able to send data in prepareforSegue , so from the yellow icon of VC1 drag to both VC2 and VC3 and give a segue identifier like toSecond & toThird respectively then in code do

// change segue identifier if you want to third 
self.performSegue(withIdentifier: "toSecond ", sender:nil) 

Upvotes: 4

Related Questions