我有一个带有名为 BasicPhrasesVC 的 TableView 的 ViewController,我想传递所选单元格中的数据以在下一个 ViewController(称为 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?) {
我不确定在这里放什么(我想传递变量“selectedBasicPhrase”)
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedBasicPhrase = basicPhrases[indexPath.row]
performSegueWithIdentifier("BasicPhrasesVC2BasicDisplayVC", sender: self)
}
}
感谢任何帮助。
最佳答案
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
}
}
}
关于ios - 如何在 swift 中使用 prepareForSegue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173734/