5

I have a ViewController with tableview called BasicPhrasesVC and I want to pass the data in the selected cell to display it on the next ViewController (called BasicPhrasesVC).

class BasicPhrasesVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

let basicPhrases = ["Hello.","Goodbye.","Yes.","No.","I don't understand.","Please?","Thank you.","I don't know."]
var selectedBasicPhrase = ""

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return basicPhrases.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
    cell.textLabel?.text = basicPhrases[indexPath.row]
    return cell
}

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

I am unsure of what to put here (I want to pass on the variable "selectedBasicPhrase")

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedBasicPhrase = basicPhrases[indexPath.row]
    performSegueWithIdentifier("BasicPhrasesVC2BasicDisplayVC", sender: self)

}
}

Any help is appreciated.

D. Lin
  • 73
  • 1
  • 1
  • 4

3 Answers3

9
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedBasicPhrase = basicPhrases[indexPath.row]
    self.performSegueWithIdentifier("BasicPhrasesVC2BasicDisplayVC", sender: selectedBasicPhrase)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "BasicPhrasesVC2BasicDisplayVC" {
            if let nextVC = segue.destinationViewController as? NextViewController {
                nextVC.selectedBasicPhrase = sender
            }
        }
    }
0
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "<segue name>") {
        // pass the data
    }
}
Ujjwal Khatri
  • 716
  • 1
  • 6
  • 19
0

Check segue name than take destinationViewController and send you're data to it.

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "BasicPhrasesVC2BasicDisplayVC") {
        let viewController:ViewController = segue!.destinationViewController as ViewController
        viewController.selectedBasicPhrase = "Test phrase"
    }
}
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • No problem always happy to help – Oleg Gordiichuk Mar 23 '16 at 09:13
  • I now have this: `override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if (segue.identifier == "BasicPhrasesVC2BasicDisplayVC") { let viewController:BasicPhrasesVC = segue.destinationViewController as! BasicDisplayVC viewController.selectedBasicPhrase = basicPhrases[indexPath.row] } }` With the error "Cannot Convert value of type 'BasicDisplayVC' to specified type 'BasicPhrasesVC'" – D. Lin Mar 23 '16 at 09:22