【发布时间】:2018-07-06 09:14:16
【问题描述】:
我有一个如下的故事板:
单击第一个字段(选择表单)时,第二个视图将显示为弹出框。我在弹出框表格视图中有一组值作为表格单元格。 因此,当我单击 tableview 中的任何单元格时,一旦禁用弹出框,我需要在第二个文本框(NewFormName)中使用该值。
我在 MainViewController 中获得了价值。但无法使用它来更改文本框的值。
这是我的弹出框 didselectanyrow 代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var dict = FormList[(indexPath as NSIndexPath).row]
let formname:String = dict.form_name!;
/*created an instance called
'MainViewControllerInstance' of MainViewController earlier*/
MainViewControllerInstance.SecondTextBox.text = formname
self.dismiss(animated: true, completion: nil)
}
这显示一个错误:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
有什么解决办法吗?一旦弹出框被解除,我需要将弹出框表格单元的选定值添加到第二个文本框。
【问题讨论】:
-
使用闭包作为变量并获取值
-
我认为你不应该假设 dict.form_name 不为零。
-
你不能
MainViewControllerInstance.SecondTextBox.text直接在SecondVC中取变量赋值 -
尝试检查 FormList 本身是否为 nil。
-
值实际上不是零。我可以在主视图控制器的控制台中打印值。但无法在 TextBox.text 中使用它