我是 Swift 开发的新手。单击注释 View 时,我需要帮助来传递数据。单击注释 View 时,它会转到名为 DetailsViewController
的 View Controller ,但不会传递数据。任何人都可以通过在我的 View Controller 中传递一些数据/细节来帮助我解决这个问题。感谢您的帮助。 PS:我从其他开发人员那里学习代码。 :-)。
这是我的代码:
import UIKit
import MapKit
protocol UserLocationDelegate {
func userLocation(latitude ouble, longitude ouble)
}
class NearMeMapViewController: ARViewController, ARDataSource, MKMapViewDelegate, CLLocationManagerDelegate {
var nearMeIndexSelected = NearMeIndexTitle ()
var locationManager : CLLocationManager!
var nearMeARAnnotations = [ARAnnotation]()
var nearMeRequests = [NearMeRequest]()
var delegate : UserLocationDelegate!
var place: Place?
override func viewDidLoad() {
super.viewDidLoad()
self.title = nearMeIndexSelected.indexTitle
self.locationManager = CLLocationManager ()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLHeadingFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.dataSource = self
self.headingSmoothingFactor = 0.05
self.maxVisibleAnnotations = 30
getNearMeIndexSelectedLocation()
}
func getNearMeIndexSelectedLocation()
{
let nearMeRequest = MKLocalSearchRequest()
nearMeRequest.naturalLanguageQuery = nearMeIndexSelected.indexTitle
let nearMeregion = MKCoordinateRegionMakeWithDistance(self.locationManager.location!.coordinate, 250, 250)
nearMeRequest.region = nearMeregion
let nearMeSearch = MKLocalSearch(request: nearMeRequest)
nearMeSearch.start { (response : MKLocalSearchResponse?, error :Error?) in
for requestItem in (response?.mapItems)! {
let nearMeIndexRequest = NearMeRequest ()
nearMeIndexRequest.name = requestItem.name
nearMeIndexRequest.coordinate = requestItem.placemark.coordinate
nearMeIndexRequest.address = requestItem.placemark.addressDictionary?["FormattedAddressLines"] as! [String]
nearMeIndexRequest.street = requestItem.placemark.addressDictionary?["Street"] as! String!
nearMeIndexRequest.city = requestItem.placemark.addressDictionary?["City"] as! String
nearMeIndexRequest.state = requestItem.placemark.addressDictionary?["State"] as! String
nearMeIndexRequest.zip = requestItem.placemark.addressDictionary?["ZIP"] as! String
self.nearMeRequests.append(nearMeIndexRequest)
print(requestItem.placemark.name)
}
for nearMe in self.nearMeRequests {
let annotation = NearMeAnnotation(nearMeRequest: nearMe)
self.nearMeARAnnotations.append(annotation)
self.setAnnotations(self.nearMeARAnnotations)
}
}
}
更新你的 tapBlurButton 功能
func tapBlurButton(_ sender: UITapGestureRecognizer) {
if let annotationView = sender.view as? NearMeARAnnotationView {
if let detailsVc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController")
as? DetailsViewController {
detailsVc.place = Place(location: (locationManager.location)!,
reference: "",
name: annotationView.annotationNameLabel.text ?? "",
address: annotationView.annotationAddressLabel.text ?? "")
self.navigationController?.pushViewController(detailsVc, animated: true)
}
}
}
更新您的 NearMeARAnnotationView 类
class NearMeARAnnotationView: ARAnnotationView, CLLocationManagerDelegate {
var annotation: ARAnnotation! //<= ADD this variable.
//>>Other code here
init(annotation : ARAnnotation) {
super .init()
self.annotation = annotation //<= pass data to your new var
//>>Other initialization code here
}
//>> Other code here
}
更新您的详细信息 vc 代码
class DetailsViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{
var annotation: ARAnnotation!
//>> Other code here
}
更新你的新的 tapBlurButton 函数
func tapBlurButton(_ sender: UITapGestureRecognizer) {
if let annotationView = sender.view as? NearMeARAnnotationView {
if let detailsVc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController")
as? DetailsViewController {
detailsVc.annotation = annotationView.annotation
detailsVc.place = Place(location: (locationManager.location)!,
reference: "",
name: annotationView.annotationNameLabel.text ?? "",
address: annotationView.annotationAddressLabel.text ?? "")
self.navigationController?.pushViewController(detailsVc, animated: true)
}
}
}
然后在您的 DetailsVC 中,您应该能够访问通过这种方式传递给 NearMeARAnnotationView 的注释中的所有数据:
let name = annotation.name
let address = annotation.address
直接在DetailsVC中
关于ios - 谁能帮我在 Swift 3 中传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47065211/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |