如何添加注释以从另一个视图控制器映射
How to add annotation to map from another View Controller
我想使用 CreateDetailsViewController
中的按钮创建注释以将其添加到 MapViewController
。
当我将下面的代码输入到 MapView 的 viewDidLoad
中时,它工作正常。但是我不知道如何实现它从一个屏幕到另一个屏幕。
我对编程很陌生,所以请在回答时考虑这一点。谢谢!
//Button I want to create the annotation with in `CreateDetailsViewControllwer` :
@IBAction func publishButtonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToMap", sender: self)
func prepare (for segue: UIStoryboardSegue, sender: Any?) {
let MapVC = segue.destination as! MapViewController
var london = MKPointAnnotation()
london.title = "London"
london.coordinate = CLLocationCoordinate2DMake(51.508530, -0.076132)
MapVC.mapView.addAnnotation(london)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else {return nil}
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
你好 Selen,你需要使用通知中心
通知中心:您可以将数据从一个视图控制器广播到另一个视图控制器。
@IBAction func publishButtonPressed(_ sender: UIButton) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "pin koy"), object: nil)
performSegue(withIdentifier: "goToMap", sender: self)
}
然后使用这段代码到你想要创建注释的地方(我认为是mapViewController)
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(pinleme), name: NSNotification.Name(rawValue: "pin koy"), object: nil)
}
它是您注释的函数;
@objc func pinleme() {
let annotation= MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(51.508530, -0.076132)
annotation.title = "London"
mapKit.addAnnotation(annotation)
//anlamadıysan inst-fburakk19
}
我想使用 CreateDetailsViewController
中的按钮创建注释以将其添加到 MapViewController
。
当我将下面的代码输入到 MapView 的 viewDidLoad
中时,它工作正常。但是我不知道如何实现它从一个屏幕到另一个屏幕。
我对编程很陌生,所以请在回答时考虑这一点。谢谢!
//Button I want to create the annotation with in `CreateDetailsViewControllwer` :
@IBAction func publishButtonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToMap", sender: self)
func prepare (for segue: UIStoryboardSegue, sender: Any?) {
let MapVC = segue.destination as! MapViewController
var london = MKPointAnnotation()
london.title = "London"
london.coordinate = CLLocationCoordinate2DMake(51.508530, -0.076132)
MapVC.mapView.addAnnotation(london)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else {return nil}
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
你好 Selen,你需要使用通知中心
通知中心:您可以将数据从一个视图控制器广播到另一个视图控制器。
@IBAction func publishButtonPressed(_ sender: UIButton) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "pin koy"), object: nil)
performSegue(withIdentifier: "goToMap", sender: self)
}
然后使用这段代码到你想要创建注释的地方(我认为是mapViewController)
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(pinleme), name: NSNotification.Name(rawValue: "pin koy"), object: nil)
}
它是您注释的函数;
@objc func pinleme() {
let annotation= MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(51.508530, -0.076132)
annotation.title = "London"
mapKit.addAnnotation(annotation)
//anlamadıysan inst-fburakk19
}