UIPickerView将数据传递到容器视图

时间:2019-06-10 08:54:19

标签: ios swift uipickerview

我在StacksOverflow上浏览了一些容器视图问题,但是没有人使用选择器视图将数据传递到Containerview中的tableview单元格。我尝试使用:

    func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "containerSettingsSegue" {
            let destination = segue.destination as! SettingsTableViewController
            destination.delegate = self
            destination.languageDetail.text = myPickerData[row]
        }
    }
}

还有

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "containerSettingsSegue" {
        let destination = segue.destination as! SettingsTableViewController
        destination.delegate = self
    }
}

以上两个代码都在我的父视图中。

但是,它不起作用。有什么办法可以做到吗?

1 个答案:

答案 0 :(得分:1)

感谢@TBlank回答https://stackoverflow.com/a/34349405/1142743

因此,根据他的回答代码,如下所示:

SettingsTableViewController中声明SettingsViewController的实例,例如:

var childView : SettingsTableViewController?


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "containerSettingsSegue" {
            childView = segue.destination as? SettingsTableViewController
            childView!.delegate = self
        }


     }


func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            childView?.changeLabel(text: myPickerData[row])
    }

现在在SettingsTableViewController中声明一个方法,如下所示

func changeLabel(text: String) {
        languageDetail.text = text
    }