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.
Notifies the view controller that a segue is about to be performed. iOS 5.0+ iPadOS 5.0+ Mac Catalyst 13.1+ tvOS 9.0+
In your storyboard, create an unwind segue by right-clicking a triggering object and dragging to the Exit control at the top of your view controller's scene.
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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With