我有两个 VC,我正在尝试使用 segue 将数据从第一个 VC 传递到第二个。第一个 VC 有一个 TableView ,每个单元格包含一个标题和一个使用核心数据存储的描述标签。我想详细了解第二个 VC 中的每个单元格。我已经初始化了两个字符串来存储第一个 VC 中的值,并将它们分配给第二个 VC 中的字符串。
请看下面的代码:
var SelectedTitle = String()
var SelectedDisc = String()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
SelectedTitle = self.listAthkar[indexPath.row].title!
print(SelectedTitle)
SelectedDisc = self.listAthkar[indexPath.row].details!
print(SelectedDisc)
performSegue(withIdentifier: "showCell", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showCell") {
var destinationVC = segue.destination as! showCellVC
destinationVC.passedTittle = SelectedTitle
destinationVC.passedDisc = SelectedDisc
}
}
class showCellVC: UIViewController {
var passedTittle = String()
var passedDisc = String()
@IBOutlet weak var cellTitle: UILabel!
@IBOutlet weak var cellDisc: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
cellTitle.text = passedTittle
cellDisc.text = passedDisc
print("Title Passed is \(passedTittle)")
print("Discription Passed is \(passedDisc)")
}
当我测试应用程序时,控制台打印:
Title Passed is
Discription Passed is
title
Description
这是实际单元格的图片
最佳答案
看起来您在 Storyboard 中的 tableview 单元格上放置了一个 action
segue。由于您使用的是程序化转场,因此您不希望这样。 Action 转场在您的 didSelect
方法之前触发,因此转场发生在变量设置之前。
关于ios - VC之间传递数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45917850/