我无法在另一个视图控制器中执行功能

时间:2019-11-09 14:55:47

标签: ios swift function viewcontroller

有人可以帮助我从另一个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()
} 

如果我通过第一个VC(其中带有directionToPin函数的VC)中的按钮运行相同的功能,则当我运行该应用并按下按钮时, currentPlacemark 为零。

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

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为您需要使用协议和代表来实现您想要的目标。

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

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

我的建议是创建一个新协议,像这样从 MapViewController CardViewController 通信

将它们添加到 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()
    }
}