• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:10:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是 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)




      }

    }

  }



Best Answer-推荐答案


更新你的 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap