2

I have a BaseViewController connected to 3 different custom ViewControllers using segues with Identifiers. The BaseViewController has a tableview and based on the data in the tableviewCell, the BaseViewController decides which segue to take to the one of 3 custom DestinationViewControllers.

I also need to pass some data to the DestinationViewController.

I am trying to use either prepareForSegue or performSegueWithIdentifier.

  1. prepareForSegue allows me to use the UIStoryboardSegue to set the destination view controller and pass it data. However, it does not let me specify the segue identifier I would like to call. Case 0 & Case 1 use two different ViewControllers.

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        switch testVar{
    
        case 0:  
            let svc = segue.destinationViewController as! CustomTableViewController1
            svc.name = myName
            break;
    
        case 1:  
            let svc = segue.destinationViewController as! CustomTableViewController2
            svc.name = myName1
    
            break;
    
        default:
            break
        }
    }
    
  2. performSegueWithIdentifier allows me to specify the segue identifier, but it does not allow me to specify which one of the 3 custom DestinationViewControllers i'd like to call nor does it give me a good way to pass the data i need to pass.

    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    
      switch testVar {
    
          case 0:  
            performSegueWithIdentifier("destination1", sender:self )
            break;
    
          case 1:  //Submitted
            performSegueWithIdentifier("destination1", sender: self)
            break;
    
          default:
            break
          }
    
      return indexPath
    }
    

Can you guys propose any solutions ? Thanks in advance!

vadian
  • 274,689
  • 30
  • 353
  • 361
branimal
  • 97
  • 9
  • In `prepareForSegue(_:)` you can get the identifier using: `segue.identifier`. You could just use both methods. Call `performSegueWithIdentfier(_:)` and then pass the data in `prepareForSegue(_:)`. Also, in swift switches, you don't need to call `break` explicitly. – Eendje Apr 09 '16 at 19:19
  • ... and you don't need any semicolons. – vadian Apr 09 '16 at 19:25
  • You probably will use **both** performSegueWithIdentifier and prepareForSegue. In your action call performSegueWithIdentifier and then in prepareForSegue, use if statements to do different actions for the different segues and destination controller. – ryantxr Apr 09 '16 at 19:29

1 Answers1

0

You have to use both performSegue and prepareforsegue

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

   switch testVar { 
       case 0:       
                     performSegueWithIdentifier("destination1", sender:self ) 
           break; 
       case 1: //Submitted          
                  performSegueWithIdentifier("destination1", sender: self) 
           break; 
       default: 
           break 
    } 
    return indexPath 
}

And in your prepareForSegue implement as below:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "destination1" {
        if let svc = segue.destinationViewController as? CustomTableViewController1 {
            svc.name = myName
        }
    } else if segue.identifier == "destination2" {
        if let svc = segue.destinationViewController as? CustomTableViewController2 {
            svc.name = myName1
        }
    }
}

Also use didSelectRow instead of willSelectRow if required

Arun Gupta
  • 2,628
  • 1
  • 20
  • 37