关于ios:在SWIFT中关闭Popover表视图后,传递值

Pass value once Popover table view is dismissed in SWIFT

我有一个类似以下的分镜脚本:

My StoryBoard

单击第一个字段("选择表单")后,第二个视图将显示为弹出窗口。在弹出菜单视图中,我有一组值作为表格单元格。
因此,当我单击表视图中的任何单元格时,一旦禁用弹出框,我就需要在第二个文本框(NewFormName)中使用该值。

我在MainViewController中获得了价值。 但是无法使用它来更改文本框的值。

这是我的弹出框didselectanyrow代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
  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)

}

这显示了一个错误:

1
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

有什么办法吗? 一旦关闭弹出框,我需要在第二个文本框中输入弹出框的选定值。


在您的PopoverViewController中:

1
2
3
4
5
6
7
8
9
10
var delegate: MainViewController?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    var dict = FormList[(indexPath as NSIndexPath).row]
    let formname: String? = dict.form_name
    delegate?.updateTextBox(with: formname)
    self.dismiss(animated: true, completion: nil)

}

在您的MainViewController中:

1
2
3
4
5
6
7
8
9
10
11
12
func updateTextBox(with string: String?) {
    if string != nil {
        self.SecondTextBox.text = string!
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier =="popoverSegue" {
            let popoverViewController = segue.destinationViewController as! PopoverViewController
            popoverViewController.delegate = self
        }
    }


使用委托或闭包将值传递回最后一个对象