muta BLW
muta BLW

Reputation: 1

How to run a segue on itself OR another viewController

I have a main TableViewController. When the user tap on a cell in TableView, If a condition statement is true, show the main TableViewController again(but show another data on screen), and if the condition is false, show another ViewController. In the prepareForSegue method I have this:

if  segue.identifier == "identifier1"
{
    let destination = segue.destinationViewController as? mainViewController // itself
    destination!.x = something // the x variable is in this view controller
}
else if segue.identifier == "identifier2"
{
    let destination = segue.destinationViewController as? viewController2
    destination!.y = something
}

Also, to run segue after tap on cells in tableView I added this:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if condition is true {
        self.performSegueWithIdentifier("identifier1", sender: self)
    }
    else{
        self.performSegueWithIdentifier("identifier2", sender: self)
    }
}

Storyboard segue needs an identifier... It can just have one value! identifier1 or identifier2 But I want to set the identifier programmatically. How should I set the identifier in Storyboard segue? Is there another way to do this? Thanks!

Upvotes: 0

Views: 121

Answers (2)

vacawama
vacawama

Reputation: 154691

The destination view controller is set by the segue. Since you have two different destinations, you need two different segues in your Storyboard.

The trick is to wire the segues from the viewController icon at the top of tableViewController to each of the destination view controllers. Click on the resulting segue arrows and set their identifiers in the Attributes Inspector.

wire segue in Storyboard

Then you can call them with performSegueWithIdentifier as you have done in your code.

Upvotes: 1

Duncan C
Duncan C

Reputation: 131481

Don't invoke a segue directly from the cell tap. Use an IBAction method (probably in `didSelectRowAtIndexPath, like you show in the second block of code.

That second block of code DOES set the segue identifier programmatically.

You can't switch segues in prepareForSegue.

Upvotes: 0

Related Questions