任何人都可以帮我传递Swift 3中的数据吗?

时间:2017-11-01 23:25:23

标签: ios swift swift3

我是Swift开发的新手。单击注释视图时,我需要帮助来传递数据。单击注释视图时,它将转到名为DetailsViewController的视图控制器,但不会传递数据。任何人都可以通过在我的视图控制器中传递一些数据/细节来帮助我解决这个问题感谢您的帮助。 PS:我从其他开发人员代码中学习。 :-)。

这是我的代码:

import UIKit
import MapKit

protocol UserLocationDelegate {
  func userLocation(latitude :Double, longitude :Double)
}

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)




      }

    }

  }

1 个答案:

答案 0 :(得分:0)

更新你tapBlurButton func

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