我无法在另一个 ViewController 中执行函数

I can't execute functions from a ViewController in an different one

谁能帮我在另一个 VC 中执行一个 VC 的功能。 当我按下第二个 VC 中的按钮时,需要执行第一个 VC 中的函数。 我正在尝试使用 "viewcontroller().function()" 功能,但它无法正常工作,打印和基本功能可以使用,但是当涉及到绘图方向等功能时,它无法正常工作。

绘制方向的函数是:

func directionToPin() {

    guard let currentPlacemark = currentPlacemark else {
        print("Error, the current Placemark is: \(self.currentPlacemark)")
        return
    }

    let directionRequest = MKDirections.Request()
    let destinationPlacemark = MKPlacemark(placemark: currentPlacemark)

    directionRequest.source = MKMapItem.forCurrentLocation()
    directionRequest.destination = MKMapItem(placemark: destinationPlacemark)
    directionRequest.transportType = .walking

    //calculate route
    let directions = MKDirections(request: directionRequest)
    directions.calculate{ (directionsResponse, error) in

        guard let directionsResponse = directionsResponse else {
            if let error = error {
                print("error getting directions: \(error.localizedDescription)")
            }
            return
         }

        let route = directionsResponse.routes[0]

        if self.drawedDriection == false {
            self.drawedDriection = true

            if self.didSelectAnnotation == true {
                self.mapView.addOverlay(route.polyline, level: .aboveRoads)self.navigationBarController.directionButtonOutlet.setImage(UIImage(named: "navigationBarDirectionButtonRed")?.withRenderingMode(.alwaysOriginal), for: .normal)
                        self.mapView.setRegion(MKCoordinateRegion(routeRect), animated: true)
                    }
                } else {
                    self.drawedDriection = false
                    self.mapView.removeOverlays(self.mapView.overlays)
                    if self.didSelectAnnotation == true {
                        self.navigationBarController.directionButtonOutlet.setImage(UIImage(named: "navigationBarDirectionButtonBlue")?.withRenderingMode(.alwaysOriginal), for: .normal)
                    } else {
                        self.navigationBarController.directionButtonOutlet.setImage(UIImage(named: "navigationBarDirectionButtonGray")?.withRenderingMode(.alwaysOriginal), for: .normal)
                    }
                }
            }
        }

我在按下按钮后调用第二个函数 VC:

@IBAction func directionButton(_ sender: Any) {
    MapViewController().directionToPin()
} 

当我 运行 应用程序并按下按钮时 currentPlacemark 为零,如果我 运行 通过我的第一个按钮执行相同的功能 VC(里面有directionToPin函数的VC)

如果你需要的话,这里是我的仓库:https://github.com/octavi42/xCodeMapsApp

谢谢!

我认为你需要使用协议和代理来实现你想要的。

@IBAction func directionButton(_ sender: Any) {
    MapViewController().directionToPin()
} 

在上面的代码片段中,您正在实例化 MapViewController 的新实例,它在初始化时重置 currentPlacemark 因此您遇到了

我的建议是创建一个新的协议来从 MapViewControllerCardViewController 进行通信,就像这样

将这些添加到 MapViewController.swift

protocol MapNavigationDelegate: AnyObject {
    func didTapDirectionButton()
}

class MapViewController: UIViewController {
    // .... Some code ....

    override func viewDidLoad() {
        // . .... Some more code .......
        navigationBarController.mapNavigationDelegate = self
    }
}

extension MapViewController: MapNavigationDelegate {
    func didTapDirectionButton() {
        self.directionToPin()
    }
}

将这些添加到 CardViewController.swift

class CardViewController: UIView {
    // .... Some Code ....
    weak var mapNavigationDelegate: MapNavigationDelegate!

    @IBAction func directionButton(_ sender: Any) {
        self.mapNavigationDelegate.didTapDirectionButton()
    }
}