Storyboard Tableview with Segues to Multiple Views

TableView with Segues to Multiple Views

The logic that you need to implement is:

  1. Check the the segmentedIndex to know whether the user wants to see Upcoming or Showing.
  2. Load the tableView with movies per step 1.
  3. When the user taps on cell, use the segmentedIndex to identify whether you should a movie from the Upcoming or Showing array was chosen.
  4. Get the ID of that movie and send it to the respective DetailsViewController. Now that you have the movieID, use it to request the movie details from your database.

    func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as? TableViewCell else { return UITableViewCell() }

    switch segmentedControl.selectedSegmentIndex {
    case 0:
    //handle the set up of Upcoming cells

    case 1:
    //handle the setup of Showing cells

    default:
    break
    }
    return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    switch segmentedControl.selectedSegmentIndex {

    case 0:
    let DetailViewUpcoming = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewUpcoming") as! DetailViewUpcoming

    DetailViewUpcoming.init(forMovie: upcomingArray[indexPath.row].movieID)
    self.navigationController?.pushViewController(DetailViewUpcoming, animated: true)



    case 1:
    let DetailViewShowing = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewShowing") as! DetailViewShowing

    DetailViewShowing.init(forMovie: showingArray[indexPath.row].movieID)
    self.navigationController?.pushViewController(DetailViewShowing, animated: true)


    default:
    break

    }
    }


    @IBAction func segmentedTapped(_ sender: Any) {
    tableView.reloadData()
    }

Inside DetailViewUpcoming and DetailViewShowing declare a variable for the movieID and add an initialiser

var movieID: String?

init(movieID:String){
self.movieID = movieID
}

Now when you load the view use the above movieID to request the movie details and assign them to properties as you see fit.

Storyboard TableView with Segues to Multiple Views

The method
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) is called when an item is deselected.

To call a method when an item is selected, change the method name to override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

IOS StoryBoard multiple Segue's from a TableCell

Don't try to hook up the Segues to a tableviewcell in this case. Hook them up to the View Controller itself.

How to set multiple segue ViewController in TableViewController?


  1. Make a segue from the controller not from the cell
  2. call a didselectrow atindexpath method of tableview
  3. call a prepare for segue with the identifier you have set for different segues

  4. you are done

Can you have two Segues from different View Controllers going to one View Controller Swift 3

Yes, you are allowed to have several segues going from different View controllers to a single View controller.

The problem is you have either not set up an identifier for that particular view controller or you have a typo in the identifier in your code.

How to use Storyboards with multiple segues to one view controller?

Every ViewController should have their own seque. To create, press "Ctrl" and move to the ViewController you want

Sample Image

And every ViewController should implement "prepareForSegue"

- (void)prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[MyDestinationVC class]]) {
//do something
//Example
MyDestinationVC *screen = (MyDestinationVC *)segue.destinationViewController;
screen.someValue = self.sendedValue;
}
}

How do I use a table view to go to different view controllers?

Yes, didSelectRowAt function is the best way to identify which cell or row is selected at the time.

  • didSelectRowAt Function should be like below

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
    performSegue(withIdentifier: "FirstViewController", sender: nil)
    } else if indexPath.row == 1 {
    performSegue(withIdentifier: "SecondViewController", sender: nil)
    }
    }
  • Segue Prepare Function Should be Like Below

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "FirstViewController" {
    let vc = segue.destination as? FirstViewController
    } else if segue.identifier == "SecondViewController" {
    let vc = segue.destination as? SecondViewController
    }

Hope, It Helps :)



Related Topics



Leave a reply



Submit