将文本从MKMarkerAnnotation传递给不同的视图控制器

时间:2018-05-15 01:27:10

标签: swift firebase uiviewcontroller mkmapview mkannotationview

我试图在点击标记时将MKMapView上的标记文本传递给不同的视图控制器。我在线阅读后尝试使用一种方法,但由于视图控制器我将文本传递给没有导航控制器,我认为它不会起作用。请注意,我还使用Firebase来检索将在标记上显示的数据以及视图控制器看起来奇怪的原因是因为我正在使用ISHPullUp

视觉表现:https://imgur.com/a/zEW64aY

TopViewController的代码(MKMapView和MKMarker数据)

var mainTitle = ""

Database.database().reference().child("posts").observeSingleEvent(of: .value, with: { (snapshot) in
        for snap in snapshot.children {
            let postSnap = snap as! DataSnapshot
            if let dict = postSnap.value as? [String:AnyObject] {
                let title = dict["title"] as! String
                let locationName = dict["location"] as! String
                let discipline = dict["category"] as! String
                let coordinate = CLLocationCoordinate2D(latitude: dict["lat"] as! Double, longitude: dict["long"] as! Double)
                let artwork = Artwork(title: title, locationName: locationName, discipline: discipline, coordinate: coordinate)
                self.mapView.addAnnotation(artwork)
            }
        }
    })

extension TopViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard let annotation = annotation as? Artwork else { return nil }
        let identifier = "marker"
        var view: MKMarkerAnnotationView
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
            as? MKMarkerAnnotationView {
            dequeuedView.annotation = annotation
            view = dequeuedView
        } else {
            view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            mainTitle = annotation.title!
        }
        return view
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        let myVC = storyboard?.instantiateViewController(withIdentifier: "bottom") as! BottomViewController // Didn't work
        myVC.stringPassed = mainTitle
        navigationController?.pushViewController(myVC, animated: true)
        print(mainTitle)
    }
}

BottomViewController的代码(查看控制器我试图将文本发送到)

var stringPassed = ""

override func viewDidLoad() {
    super.viewDidLoad()

    toplabel.text = stringPassed
}

来自ViewController的代码

class ViewController: ISHPullUpViewController {
    private func commonInit() {
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let contentVC = storyBoard.instantiateViewController(withIdentifier: "content") as! TopViewController
        let bottomVC = storyBoard.instantiateViewController(withIdentifier: "bottom") as! BottomViewController
        contentViewController = contentVC
        bottomViewController = bottomVC
        bottomVC.pullUpController = self
        contentDelegate = contentVC
        sizingDelegate = bottomVC
        stateDelegate = bottomVC
    }
}

1 个答案:

答案 0 :(得分:1)

您需要实施didSelect

MKMapViewDelegate委托方法
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if view.annotation is MKUserLocation {
            return
        }
        let ann = view.annotation as! Artwork
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let bottomVC = storyBoard.instantiateViewController(withIdentifier: "bottom") as! BottomViewController
        bottomViewController = bottomVC
        bottomVC.pullUpController = self
        sizingDelegate = bottomVC
        vc.stringPassed = ann.title
}

//

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if segue.identifier == "bottom" {
      let nextScene =  segue.destination  as! BottomViewController 
      nextScene.stringPassed = sender as! String
    }
}

//

也可以在push

中使用TopViewController代替segue嵌入UINavigationController
相关问题