swiftsegueviewcontrollerprepare

Swift prepareForSegue to one of many custom ViewControllers


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!


Solution

  • 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