简体   繁体   中英

How to pass Data from a UITableViewController to another UITableViewController? -iOS & Swift 2

Let's say the setup is

Navigation Controller -> GradLevelUITableViewController -> DegreeUITableViewController -> View Controller

where:

  • Data structure is like this one .
  • GradLevelUITableViewController contains 3 cells namely Undergrad , Grad , PostGrad .
  • DegreeUITableViewController displays Colleges as sections and Degrees as rows.
  • View Controller contains a scrollable TextView for more info.

My question is, how do I pass data (particularly a list that contains info about the selected degree to be passed again later on to the TextView AND what was selected ) from GradLevelUITableViewController to DegreeUITableViewController and tell DegreeUITableViewController that I clicked one of the three cells and show its corresponding Colleges and Degrees (eg if Undergrad was tapped, show only Colleges and Degrees for Undergrad ).

I'm new to iOS and Swift Programming, I can't find a decent tutorial on passing data between View Controllers using Swift. Probably I don't know the exact term for this particular "connection".

I've tried these links:

and other tutorials that are mostly written in Objective-C. The 1st and 2nd link seems to be the answer but I'm not sure how to use it and how to pass what I need.

Something of this kind should do the trick

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if let grad = self.grads[indexPath.row] {
                self.performSegueWithIdentifier("pushDegree", sender: grad)
        }
    }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "pushDegree" {
            guard
                let degreeVC = segue.destinationViewController as? DegreeViewController,

            else {
                return
            }
            degreeVC.grad = sender as! Grad
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM