从Xcode中的注释映射访问结构参数

我是快速编程的新手,我正在尝试构建一个在地图上显示一些图钉的应用程序。当用户单击图钉时,将带有按钮的注释。如果单击该按钮,它将在Safari中打开一个URL。

在完成一些教程之后,我设法拥有了一个带有大头针和按钮的功能性应用程序,但是我无法打开URL。

这是我用来构建应用程序的代码:

struct WebCam {
  var name: String
  var latitude: CLLocationDegrees
  var longitude: CLLocationDegrees
  var url: String
}

class FirstViewController: UIViewController,CLLocationmanagerDelegate {

@IBOutlet weak var mapView: MKMapView!

let webcams = [WebCam(name: "WH1",latitude: 51.5549,longitude: -0.108436,url: "www.test.it"),WebCam(name: "WH2",latitude: 51.4816,longitude: -0.191034,url: "www.google.it")]

func fetchWebcamsOnmap(_ webcams: [WebCam]) {
  for webcam in webcams {
    let annotations = MKPointAnnotation()
    annotations.title = webcam.name
    annotations.coordinate = CLLocationCoordinate2D(latitude: webcam.latitude,longitude: webcam.longitude)
    mapView.addAnnotation(annotations)
  }
}

extension FirstViewController: mkmapviewdelegate {

func mapView(_ mapView: MKMapView,viewFor annotations: MKAnnotation) -> 
MKAnnotationView? {
let identifier = "WebCam"
var view: MKMarkerAnnotationView
// 4
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
  as? MKMarkerAnnotationView {
  dequeuedView.annotation = annotations
  view = dequeuedView
} else {
  // 5
  view = MKMarkerAnnotationView(annotation: annotations,reuseIdentifier: identifier)
  view.canShowCallout = true
  view.calloutOffset = CGPoint(x: -5,y: 5)
    let mapsButton = UIButton(frame: CGRect(origin: CGPoint.zero,size: CGSize(width: 30,height: 30)))
    mapsButton.setBackgroundImage(UIImage(named: "webcam"),for: UIControl.State())
    view.rightCalloutaccessoryView = mapsButton
}

return view

}

这是检索UIButton按下的代码

    func mapView(_ mapView: MKMapView,annotationView view: MKAnnotationView,calloutaccessoryControlTapped control: UIControl) {
       if control == view.rightCalloutaccessoryView {

       }
   }

我试图通过使用

访问选定网络摄像头的网址来进行简单的控制台打印
let web = view.annotation as! WebCam
let webURL = WebCam.url

,但显示错误。

MaiRongJian 回答:从Xcode中的注释映射访问结构参数

此演员表将崩溃

let web = view.annotation as! WebCam

您需要子类化

class CusAnn:MKPointAnnotation {
  var url:String?
}

并添加注释,例如

let annotations = CusAnn()

然后

let web = view.annotation as! CusAnn
let webURL = web.url
,

WebCam必须是MKPointAnnotation

的子
class WebCam : MKPointAnnotation {
    var url : String

    init(name : String,latitude : CLLocationDegrees,longitude : CLLocationDegrees,url : String) {
        self.url = url
        super.init()
        self.title = name
        self.coordinate = CLLocationCoordinate2D(latitude: latitude,longitude: longitude)
    }
}

然后您可以创建实例

let webcams = [WebCam(name: "WH1",latitude: 51.5549,longitude: -0.108436,url: "www.test.it"),WebCam(name: "WH2",latitude: 51.4816,longitude: -0.191034,url: "www.google.it")]

fetchWebcamsOnMap可以减少为

func fetchWebcamsOnMap(_ webcams: [WebCam]) {
    mapView.addAnnotations(webcams)
}

let web = view.annotation as! WebCam
let webURL = web.url

将起作用。

,

在您的代码中,它描述的问题还与您的代码在下面的行中

$ zip ignored-files.zip -r $(git ls-files -o -i --directory --exclude-standard)

替换

let web = view.annotation as! WebCam

let webURL = WebCam.url
本文链接:https://www.f2er.com/3153468.html

大家都在问