从闭包中使用委托传递数据

Passing data with a delegate from a closure

我有两个视图控制器,我正在尝试将数据从一个视图控制器传递到另一个。数据从 MKLocalSearch 闭包返回。但我似乎无法将我的委托方法获取到 运行。我希望有人能对此有所了解?我模拟了我正在尝试做的事情的较小版本。另外,我不使用故事板。我编码一切。这是代码...

import UIKit
import MapKit

protocol SendDataDelegate {
    func sendData(data: String)
}

class OneViewController: UIViewController {

var delegate: SendDataDelegate?

override func viewDidLoad() {
    super.viewDidLoad()

    doSearch() { coord in
        if let coord = coord {
            //When the execution gets here, coord does have
            //the values to be sent to the nexr view controller.
            self.delegate?.sendData(data: "\(coord)")
            let twoViewController = TwoViewController()
            self.present(twoViewController, animated: true)
        }
    }
}

func doSearch(completion: @escaping (CLLocationCoordinate2D?) -> Void) {

    var coord: CLLocationCoordinate2D?
    let request = MKLocalSearchRequest()

    request.naturalLanguageQuery = "New York"

    let search = MKLocalSearch(request: request)

    search.start(completionHandler: {(response, error) in

        if error != nil {
            print("Error occured in search:\(error!.localizedDescription)")
        } else if response!.mapItems.count == 0 {
            print("No matches found")
        } else {
            print("Matches found")

            coord = response?.mapItems[0].placemark.coordinate
        }
        completion(coord)
    })
  }
}

import UIKit

class TwoViewController: UIViewController, SendDataDelegate {

var myData: String = ""
var oneViewController = OneViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    oneViewController.delegate = self
}

func sendData(data: String) {
    myData = data
    print ("myData: \(myData)")
}

}

在您的 TwoViewController 中,您有一个 属性 持有 另一个 实例 OneViewController 并将其委托设置为 self.

因此,每次创建 TwoViewController 的实例时,都会创建一个 OneViewController 的新实例,但其中 none 实际上是由于显示实际OneViewController.

的视图

在您的情况下,委托模式不合适,您最好直接调用 TwoViewController 的方法:

class OneViewController: UIViewController {

    //no need to have a delegate

    override func viewDidLoad() {
        super.viewDidLoad()

        doSearch() { coord in
            if let coord = coord {
                //When the execution gets here, coord does have
                //the values to be sent to the nexr view controller.
                let twoViewController = TwoViewController()
                twoViewController.sendData(data: "\(coord)")
                self.present(twoViewController, animated: true)
            }
        }
    }

    //...    
}

class TwoViewController: UIViewController {

    var myData: String = ""
    //Creating a new instance of `OneViewController` has no meaning.

    override func viewDidLoad() {
        super.viewDidLoad()
        //Useless to set the delegate of newly created `OneViewController`.
    }

    func sendData(data: String) {
        myData = data
        print ("myData: \(myData)")
    }

}

如果您有任何理由在 TwoViewController 中使用 属性 oneViewController,请解释原因。您当前的代码没有任何解释。