菜鸟教程小白 发表于 2022-12-11 19:10:37

ios - 谁能帮我在 Swift 3 中传递数据?


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

<p>这是我的代码:</p>

<pre><code>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 = ()


var nearMeRequests = ()
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?[&#34;FormattedAddressLines&#34;] as!
      nearMeIndexRequest.street = requestItem.placemark.addressDictionary?[&#34;Street&#34;] as! String!
      nearMeIndexRequest.city = requestItem.placemark.addressDictionary?[&#34;City&#34;] as! String
      nearMeIndexRequest.state = requestItem.placemark.addressDictionary?[&#34;State&#34;] as! String
      nearMeIndexRequest.zip = requestItem.placemark.addressDictionary?[&#34;ZIP&#34;] 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)




      }

    }

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>更新你的 tapBlurButton 功能</p>

<pre><code>func tapBlurButton(_ sender: UITapGestureRecognizer) {

    if let annotationView = sender.view as? NearMeARAnnotationView {
      if let detailsVc = storyboard?.instantiateViewController(withIdentifier: &#34;DetailsViewController&#34;)
            as? DetailsViewController {

            detailsVc.place = Place(location: (locationManager.location)!,
                                    reference: &#34;&#34;,
                                    name: annotationView.annotationNameLabel.text ?? &#34;&#34;,
                                    address: annotationView.annotationAddressLabel.text ?? &#34;&#34;)
            self.navigationController?.pushViewController(detailsVc, animated: true)
      }
    }
}
</code></pre>

<p>更新您的 NearMeARAnnotationView 类</p>

<pre><code>class NearMeARAnnotationView: ARAnnotationView, CLLocationManagerDelegate {

    var annotation: ARAnnotation! //&lt;= ADD this variable.

    //&gt;&gt;Other code here

   init(annotation : ARAnnotation) {
    super .init()
    self.annotation = annotation //&lt;= pass data to your new var
   //&gt;&gt;Other initialization code here
   }
   //&gt;&gt; Other code here
}
</code></pre>

<p>更新您的详细信息 vc 代码</p>

<pre><code>class DetailsViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{
   var annotation: ARAnnotation!
   //&gt;&gt; Other code here
}
</code></pre>

<p>更新你的新的 tapBlurButton 函数</p>

<pre><code>func tapBlurButton(_ sender: UITapGestureRecognizer) {

      if let annotationView = sender.view as? NearMeARAnnotationView {
            if let detailsVc = storyboard?.instantiateViewController(withIdentifier: &#34;DetailsViewController&#34;)
                as? DetailsViewController {
                detailsVc.annotation = annotationView.annotation
                detailsVc.place = Place(location: (locationManager.location)!,
                                        reference: &#34;&#34;,
                                        name: annotationView.annotationNameLabel.text ?? &#34;&#34;,
                                        address: annotationView.annotationAddressLabel.text ?? &#34;&#34;)
                self.navigationController?.pushViewController(detailsVc, animated: true)
            }
      }
    }
</code></pre>

<p>然后在您的 DetailsVC 中,您应该能够访问通过这种方式传递给 NearMeARAnnotationView 的注释中的所有数据:</p>

<pre><code>let name = annotation.name
let address = annotation.address
</code></pre>

<p>直接在DetailsVC中</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 谁能帮我在 Swift 3 中传递数据?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47065211/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47065211/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 谁能帮我在 Swift 3 中传递数据?