草庐IT

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

coder 2024-01-28 原文

我是 Swift 开发的新手。单击注释 View 时,我需要帮助来传递数据。单击注释 View 时,它会转到名为 DetailsViewController 的 View Controller ,但不会传递数据。谁能通过在我的 View Controller 中传递一些数据/详细信息来帮助我解决这个问题。感谢您的帮助。 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)




      }

    }

  }

最佳答案

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

有关ios - 谁能帮我在 Swift 3 中传递数据?的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  6. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  7. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  8. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  9. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  10. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

随机推荐