如何使用 NavigationController 将数据从 UIPickerView 传递到上一个 ViewController

How to Pass data from UIPickerView to previous ViewController with NavigationController

我正在尝试制作一个设置屏幕,其中 ViewController A 具有 MapView 并且 ViewController A 具有将这些图钉放在地图上的功能。

我正在尝试读取 ViewController B 中的 UIPickerView 数据,并通过单击按钮将其发送到 ViewController A。在 ViewController 中,另一个名为 determineCity() 的函数将评估它收到的数据。

func determineCity(){
    switch incomingData {
    case "0":
        IzmirSelected()
        break
    case "1":
        print("San Francisco")
    default:
        print("Other")
    }
    @IBAction func setCityButtonClicked(_ sender: Any) {
    if selectedData != "-1" {
    self.delegate?.SendDataToViewController(info: selectedData)
    self.navigationController?.popToRootViewController(animated: true)
    }

我已经尝试过委托方法,它不会将数据发送到 ViewController A。ViewController A 上的代码永远不会改变。

我该怎么做才能将此数据发送到 ViewController A. 我做错了什么?

感谢您的帮助!

对于代理不工作,这意味着它要么是 nil

self.delegate?.SendDataToViewController(info: selectedData)

或分配给另一个不是当前实例的实例,另一种方法是

let all = self.navigationController!.viewControllers 
let vcB = all[all.count - 2] as! VCBName
vcB.callMethodHere()