입력했을 때 핀을 다른 탭의지도에 배치하는 위도와 경도 좌표의 사용자 입력을받을 수있는지도 앱을 만들려고합니다. My FirstVC는 사용자가 좌표를 입력 할 수있는 OtherVC에 이어지는 "Add Locations"버튼으로 구성됩니다. SecondVC는 MapView로 구성됩니다. 내 생각은 특정 좌표 배열을 갖는 것이고 새로운 좌표가이 배열에 추가됩니다. 이 배열을 MapView로 전송하는 방법을 모르기 때문에 실행이 부족합니다.Swift MKMapViewDelegate -보기 컨트롤러 간의 좌표 전송
import UIKit
import CoreLocation
class OtherVC: UIViewController {
@IBOutlet weak var latitudeField: UITextField!
@IBOutlet weak var longitudeField: UITextField!
var coordinates = [CLLocationCoordinate2D]()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addToMap(_ sender: Any) {
let lat = Double(latitudeField.text!)
let long = Double(longitudeField.text!)
self.coordinates.append(CLLocationCoordinate2D(latitude: lat!, longitude: long!))
}
}
맵뷰 MapView의 경우 :
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var coordinates = [CLLocationCoordinate2D]() {
didSet {
// Update the pins
// Since it doesn't check for which coordinates are new, it you go back to
// the first view controller and add more coordinates, the old coordinates
// will get a duplicate set of pins
for (index, coordinate) in self.coordinates.enumerated() {
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "Location \(index)"
mapView.addAnnotation(annotation)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "pinAnnotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
}
annotationView?.annotation = annotation
return annotationView
}
}
if-let 문에 break 문을 추가 했으므로 실행되고 좌표가 전송됩니다. 그러나 mapViewController 좌표를 수신 할 때 "mapView.addAnnotation (annotation)"오류가 발생합니다. 문제가 무엇인지 잘 모르겠습니다. – Kevin
오류를 게시 할 수 있습니까?, CLLocation2D가 유효한지 확인해야하는 플로트 번호가 아니라는 것을 기억하십시오. 원하는 경우 내 대답을 향상시킬 수 있습니다. –
"스레드 1 : EXC_BAD_INSTRUCTION"오류가 발생했습니다. 관련 될 수 있습니까? ClLocation2D의 수치 형 – Kevin