How do I use prepareForSegue in swift?

后端 未结 3 1022
长发绾君心
长发绾君心 2021-01-18 23:56

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

相关标签:
3条回答
  • 2021-01-19 00:26
    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 讨论(0)
  • 2021-01-19 00:27

    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"
        }
    }
    
    0 讨论(0)
  • 2021-01-19 00:43
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if (segue.identifier == "<segue name>") {
            // pass the data
        }
    }
    
    0 讨论(0)
提交回复
热议问题