【问题标题】:Layout for class班级布局
【发布时间】:2017-01-06 02:10:02
【问题描述】:

我对两个代码块之间的链接有疑问,这可能会改变注释图像。我是 swift 新手,所以我很难在 mapView 上找到它。我在函数中给出了一个代码,用于在运行时打印一个字符串,但该字符串没有显示在 NSLog 中。这是否意味着该功能根本没有运行?对于我哪里出错的任何建议,我将不胜感激。谢谢

class ViewController2: UIViewController {

@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()

//Location for first Pin
let locationOne = CLLocationCoordinate2DMake(-47.016945, 167.852095)

//Location for map centering
let locationNZ = CLLocationCoordinate2DMake(-43.937462, 170.507813)
let span = MKCoordinateSpanMake(9, 9)
let region = MKCoordinateRegion(center: locationNZ, span: span)
mapView.setRegion(region, animated: true)

//Create annotation one
let annotation = MKPointAnnotation()
annotation.coordinate = locationOne
annotation.subtitle = "Park"
annotation.title = "Rakiura National Park"

//Add annotation to the map
mapView.addAnnotation(annotation)}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()}




func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

print("func")
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil}

let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation}
 else {

let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av}

if let annotationView = annotationView {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "mapIcon.png")}

return annotationView}}

【问题讨论】:

    标签: ios swift annotations mkmapview customization


    【解决方案1】:

    听起来你可能有我忘了设置委托,所以我的委托从来没有被称为问题;当您的委托方法没有触发时,这应该始终是您所怀疑的。试试

    mapView.delegate = self
    

    【讨论】:

    • 感谢您的回复。试一试,但收到一条错误消息,提示 Value of type '(MKMapView, MKAnnotation) -> MKAnnotationView?'没有成员代表。对此有何想法?
    • 这没有意义。它表明您正在尝试将委托分配给函数。您将委托分配给 MapView 对象上的委托属性并将其指向您自己。然后实现 MapViewDelegate 协议。见developer.apple.com/library/content/documentation/General/…
    猜你喜欢
    • 2016-05-08
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多