将文本从 MKMarkerAnnotation 传递到不同的视图控制器
Pass text from MKMarkerAnnotation to different view controller
我试图在点击标记时将文本从我在 MKMapView 上的标记传递到不同的视图控制器。我在网上阅读后尝试使用一种方法,但由于我将文本传递到的视图控制器没有导航控制器,我认为它不会起作用。请注意,我还使用 Firebase 检索将显示在标记上的数据,视图控制器看起来很奇怪的原因是因为我使用的是 ISHPullUp
视觉表示:https://imgur.com/a/zEW64aY
来自顶部的代码ViewController(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)
}
}
来自底部的代码ViewController(我正在尝试将文本发送到的视图控制器)
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
}
}
您需要实现 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
而不是在 UINavigationController
中嵌入 TopViewController
我试图在点击标记时将文本从我在 MKMapView 上的标记传递到不同的视图控制器。我在网上阅读后尝试使用一种方法,但由于我将文本传递到的视图控制器没有导航控制器,我认为它不会起作用。请注意,我还使用 Firebase 检索将显示在标记上的数据,视图控制器看起来很奇怪的原因是因为我使用的是 ISHPullUp
视觉表示:https://imgur.com/a/zEW64aY
来自顶部的代码ViewController(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)
}
}
来自底部的代码ViewController(我正在尝试将文本发送到的视图控制器)
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
}
}
您需要实现 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
而不是在 UINavigationController
TopViewController