gpt4 book ai didi

ios - 如何将 .xib 界面设置为自定义标注 View ?

转载 作者:搜寻专家 更新时间:2023-10-30 22:35:49 25 4
gpt4 key购买 nike

我刚刚使用 GUI 创建了 CustomCallout.xib。我想将它用作注释的自定义标注,但我不知道应该在哪里设置要显示的 View 以及如何将值传递给该 View 。

我想显示的callout View 如下所示。 enter image description here

这是我的自定义注释 的代码。

class MapPin : MKPointAnnotation {
var name: String
var street: String
var type: String
var postCode: String

init(name: String, street: String, type: String, postCode: String) {
self.name = name
self.street = street
self.type = type
self.postCode = postCode
}}

还有我的 View Controller :

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet var mapView: MKMapView!
let span = MKCoordinateSpanMake(0.05, 0.05)


override func viewDidLoad() {
super.viewDidLoad()

self.mapView.delegate = self

let location = CLLocationCoordinate2D(
latitude: 53.430,
longitude: 14.529)

let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)

let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand")
punkt.coordinate = location

mapView.addAnnotation(punkt)
}

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
if view.annotation.isKindOfClass(MKUserLocation){
return
}

var customView = (NSBundle.mainBundle().loadNibNamed("CustomCallout", owner: self, options: nil))[0] as! CustomCallout;

var calloutViewFrame = customView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
customView.frame = calloutViewFrame;

let cpa = view.annotation as! MapPin

view.addSubview(customView)

var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: span)
self.mapView.setRegion(newRegion, animated: true)
}}

不幸的是,正在运行的应用程序显示正确创建的 pin 和正确的位置,但值来自 let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand")
没有传递给 View ,所以它看起来像这样。 enter image description here

最佳答案

您可以在 Swift 中像这样显示自定义标注 View 。

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKPinAnnotationView!)
{
if view.annotation.isKindOfClass(MKUserLocation){
return
}

var customView = (NSBundle.mainBundle().loadNibNamed("SubView", owner: self, options: nil))[0] as CustomSubView;

var calloutViewFrame = customView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
customView.frame = calloutViewFrame;

let cpa = view.annotation as CustomPointAnnotation
//You can add set vlaues for here
//cpa.title
//cpa.postCode
//cpa.street

view.addSubview(customView)

//zoom map to show callout
let spanX = 0.0000000000000001
let spanY = 0.0000000000000001

var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
self.map?.setRegion(newRegion, animated: true)
}

关于ios - 如何将 .xib 界面设置为自定义标注 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29796159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com