在didSelectAtRow时将数据传递给textFeild以便下一个viewControler

问题描述 投票:0回答:2

[当我选择单元格时尝试传递数据时,它将传递到下一个viewController,但传递数据时则不会崩溃。

如何通过使用didSelectAtRow将数据传递给文本Feild?

程序:-

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let  vc = self.storyboard?.instantiateViewController(withIdentifier: "DiscountDetailsController") as! DiscountDetailsController

     //MARK: This one is not setting data to textFeild in next view controller
     vc.offerName.text = discountArray?[indexPath.row].offerName

     //vc.offerValue.text = String((discountArray?[indexPath.row].percentage)! )
     //vc.delete.isHidden = false

     self.navigationController?.pushViewController(vc, animated: true)
}
ios swift uitextfield didselectrowatindexpath
2个回答
0
投票

您不应该尝试直接操纵另一个视图控制器的视图。这违反了封装原理。 (这是糟糕的设计,可能会导致您所描述的错误。)

相反,给DiscountDetailsController一个String属性offerNameString。在上面的didSelect方法中设置该属性,然后在DiscountDetailsController的viewWillAppear中,根据需要将offerNameString安装到视图中。

我不明白您的“ ...在传递数据时不崩溃。”声明。您的代码崩溃了吗?如果是这样,则可能是因为DiscountDetailsControllerofferName属性是一个隐式展开的可选变量,为nil。那会导致崩溃。


0
投票

您在destinationViewController中的textField在设置它的值时可能未完全初始化,因此您必须有选择地将其打开,只需在尝试设置它的text属性之前添加一个问号即可。

vc.offerName?.text = discountArray?[indexPath.row].offerName
© www.soinside.com 2019 - 2024. All rights reserved.