Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Is there a way to use conditional segue depending on which table view cell clicked

I am trying to create similar controls as the ones below the date time picker in alarm app. It appears these controls are tableview cells. I want to have several tableview cells, but when a user clicks on it, the cell's segue will take them to the View Controller of the cell clicked.

In other words, if the user clicks on the Alarm App's "Repeat" cell, the segue will take the user to the "Repeat" View Controller.

If the user clicks "Label", the cell's segue will take the user to the "Label" View Controller.

So, is it possible to have this type of conditional segue?

Any samples/links in Swift would be appreciated. Thanks in advance.

like image 630
Pavel Avatar asked Mar 12 '23 18:03

Pavel


1 Answers

in your didSelectItemAtIndexPath call different segue identifier

if indexPath.row == 0 {
    performSegueWithIdentifier("showPage1", sender: indexPath.row)
}
else if indexPath.row == 1 {
    performSegueWithIdentifier("showPage2", sender: indexPath.row)
}

Then in your prepareForSegue, check the condition.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showPage1" {
        // do something for page 1
    }
    else if segue.identifier == "showPage2" {
        // do something for page 2
    }
}

Your segue should drag from image below to destination controller enter image description here

like image 85
xmhafiz Avatar answered Apr 09 '23 01:04

xmhafiz