0

I have an app, where I have three hierarchical tableViews. The first one is a tableView with all the clubs of the user. When he selects a club he gets to the ViewController , where all the members of the selected clubs are displayed.

In the code it looks like this:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let destinationVC: UIViewController
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "memberViewController") as! MemberViewController
            destinationVC = vc
            let selectedRow = self.tableViewClub.indexPathForSelectedRow?.row
            
            vc.club = clubs[selectedRow!]
        }
        self.navigationController?.pushViewController(destinationVC, animated: true)
    }

Problem

Then I can access the members of this club in the MemberViewController. And now I want to transfer the data again but this time to another ViewController which is connected to the MemberViewControllerwith a segue.

Question

How can I transfer the members of the club to the ViewController - which is connected to the MemberViewController by a segue - since there is no selected row I can write in the code.

Community
  • 1
  • 1
  • you have the "club" property in your MemberViewController holding the data right ? pass the same to other VC what is the problem in this ? – vivekDas Aug 03 '18 at 06:36
  • 1
    You can pass data in prepare(for segue: UIStoryboardSegue, sender: Any?) in MemberViewController. While Navigating to ViewController from MemberViewController this method will be called. Please refer [segue](https://developer.apple.com/documentation/uikit/uistoryboardsegue) – Savitha Aug 03 '18 at 06:37
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Joakim Danielson Aug 03 '18 at 06:52

2 Answers2

2

In your "MemberViewController", write this code :-

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
 // Create a new variable to store the instance of ViewController 
   if let destinationVC = segue.destinationViewController as? ViewController
    destinationVC.members = members
    }
}

This method gets called when you push one viewController to another via segue. So you can pass your "members" from "MemberViewController" to "ViewController"

Nancy Madan
  • 439
  • 4
  • 7
0

You should give an identifier to segue and then try this code. in MemberViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "identifier"{
        let destinationViewController = segue.destination as? ViewController
        dectination.clob = self.club
    }

}
Sandip Gill
  • 1,060
  • 1
  • 11
  • 22