【发布时间】:2017-05-08 22:10:48
【问题描述】:
这是我的代码。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow;
let currentCell = tableView.cellForRow(at: indexPath!) as! monthTableViewCell!;
valueToPass = (currentCell?.monthOutlet.text)!
print(valueToPass)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "toMonthVC") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! monthCellViewController
// your new view controller should have property that will store passed value
viewController.dataFromHoursVC = valueToPass
}
}
所以基本上我试图将一个值从一个 VC 传递到另一个。 didSelectRow 工作正常,符合预期。但是,准备功能运行迟了。例如,第一次运行代码时,第二个 vc 将传递的值视为 nil。但是当我回去然后再做一次时,它会说传递的值,但该值是之前完成的值。所以简单地说,它就像准备函数落后或被延迟调用一样。
【问题讨论】:
-
你能出示你的
valueToPass声明吗? -
这与 prepareforswgue 被延迟调用无关,因为每当您点击后退按钮时,视图都会被删除并再次创建第二次。请展示更多内容以了解实际情况
-
您究竟在哪里访问
monthCellViewController中的dataFromHoursVC变量?
标签: swift segue collectionview