【发布时间】:2017-08-09 09:38:35
【问题描述】:
我试图将用户点击的单元格的值传递给另一个视图控制器。
这是我的代码。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
productDisplayed = currentProductList[indexPath.row] as? Product
performSegue(withIdentifier: "ProductDetailSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if ( segue.identifier == "AddProductSegue"){
let controller: AddProductViewController = segue.destination as! AddProductViewController
controller.delegate = self
}
if ( segue.identifier == "ProductDetailSegue"){
let controller: ProductDetailsViewController = segue.destination as! ProductDetailsViewController
controller.currentProduct = productDisplayed
}
}
首先,我没有写“performSegue()”。 我遇到的问题是屏幕会先被转移而不是赋值给“productDisplayed”,这意味着第二个VC中的对象“currentProduct”是nil。
然后我添加了“performSegue()”。 它仍然没有工作。问题是第二个屏幕显示了两次。第一次与上图相同。第二次是正确的。
但是当我试图点击左上角的后退按钮时。它返回到 nil 页面而不是 ProductDetail 页面。截图如下。
似乎总是先调用“准备”方法,然后调用 tableView。如何更改订单?如果可以的话,我认为应该解决这个问题。
希望能得到您的帮助。 谢谢。
【问题讨论】:
标签: ios iphone uitableview swift3 uistoryboardsegue