gpt4 book ai didi

swift - 将文本从 MKMarkerAnnotation 传递到不同的 View Controller

转载 作者:行者123 更新时间:2023-11-28 14:45:00 25 4
gpt4 key购买 nike

当点击标记时,我试图将文本从我在 MKMapView 上的标记传递到不同的 View Controller 。我在网上阅读后尝试使用一种方法,但由于我将文本传递到的 View Controller 没有导航 Controller ,我认为它不会起作用。请注意,我还使用 Firebase 检索将显示在标记上的数据, View Controller 看起来很奇怪的原因是因为我使用的是 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 的代码(我正在尝试将文本发送到的 View Controller )

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
}
}

最佳答案

你需要实现MKMapViewDelegatedidSelect委托(delegate)方法

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 而不是 segue embed TopViewController inside a UINavigationController

关于swift - 将文本从 MKMarkerAnnotation 传递到不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50341069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com