How to use prepareForSegue in swift?

I have a ViewController with a table view called BasicPhrasesVC, and I want to transfer 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'm not sure what to do (I want to pass the variable "selectedBasicPhrase")

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

Any help is appreciated.

+5
source share
3 answers
 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 } } } 
+8
source
 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if (segue.identifier == "<segue name>") { // pass the data } } 
0
source

Check the name of the segment, and not accept destinationViewController and send you the data.

 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if (segue.identifier == "BasicPhrasesVC2BasicDisplayVC") { let viewController:ViewController = segue!.destinationViewController as ViewController viewController.selectedBasicPhrase = "Test phrase" } } 
0
source

Source: https://habr.com/ru/post/1245614/


All Articles