有人可以帮我从另一个VC中执行功能吗?一旦按下第二个VC中的按钮,就需要执行第一个VC中的功能。我正在尝试使用“ viewcontroller()。function()”函数,但是它不能正常工作,打印和基本的东西都可以工作,但是当涉及到诸如绘制方向之类的东西时,它就不能工作。
绘制方向的功能是:
func directionToPin() {
//
// print("sbfdskbd")
// print("drawedDriection\(self.drawedDriection)")
// print("didSelectAnnotation\(self.didSelectAnnotation)")
guard let currentPlacemark = currentPlacemark else {
print("Error, the current Placemark is: \(self.currentPlacemark)")
return
}
print("dgvwedsvhjgdfskfbdvfjbvhdfbjvjvbvjdkbhcbvnsd")
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]
//self.mapView.removeOverlays(self.mapView.overlays)
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)
//directionButtonColor = "red"
//self.delegateChangeColors?.changeDirectionButtonColor()
let routeRect = route.polyline.boundingMapRect
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)
//directionButtonColor = "blue"
//self.delegateChangeColors?.changeDirectionButtonColor()
} else {
self.navigationBarController.directionButtonOutlet.setImage(UIImage(named: "navigationBarDirectionButtonGray")?.withRenderingMode(.alwaysOriginal), for: .normal)
//directionButtonColor = "gray"
//self.delegateChangeColors?.changeDirectionButtonColor()
}
}
}
}
按下按钮后,我将在第二个VC中调用该函数:
@IBAction func directionButton(_ sender: Any) {
MapViewController().directionToPin()
}
[运行应用程序并按按钮时,currentPlacemark为零,如果我通过第一个VC(内部具有directionToPin函数的VC)中的按钮运行相同的功能,则>]
如果需要,这是我的仓库:https://github.com/octavi42/xCodeMapsApp
谢谢!
有人可以帮我从另一个VC中执行功能吗?一旦按下第二个VC中的按钮,就需要执行第一个VC中的功能。我正在尝试使用“ viewcontroller()。function(...
我认为您需要使用协议和代表