MKAnnotationView,Swift-“提供的CUICatalog无效资产名称:''”

时间:2018-09-06 21:02:52

标签: swift annotations mkannotationview

我正在尝试在多个注释中打印子文本,这些注释显示何时点击按钮。因此,我创建了以下for循环来创建注释:

        for location in locations {
        let annotation = customAnnotation()
        annotation.title = location.title
        annotation.subtext= location.subtext
        annotation.coordinate = CLLocationCoordinate2D (latitude: location.latitude, longitude: location.longitude)
        let timeAInt= Int(location.timeA)
        let timeBInt= Int(location.timeB)
        if timeAInt... timeBInt ~= Hour { annotation.icon = "testPin1" }
        else { annotation.icon = "testPin2" }
        map.addAnnotation(annotation)
    }
    self.map.delegate = self
}

我将此类用于注释:

class customAnnotation: MKPointAnnotation {
var isCollapsed = true
var setNeedsToggle = false

var subtext: String = ""
var icon: String = ""   }

并且我使用此函数来描述pins属性:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard let annot = annotation as? customAnnotation else { return nil }

    if annotation is MKUserLocation {
        return nil
    }
    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotation")
    annotationView.image = UIImage(named: annot.icon)
    annotationView.canShowCallout = true

    let rightButton = UIButton(type: .infoLight)
    rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
    annotationView.rightCalloutAccessoryView = rightButton

    if (annot.isCollapsed) {
        annotationView.detailCalloutAccessoryView = nil
    }
    else {
        print(annot.subtext)
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        label.text = "any text"
        label.font = UIFont.italicSystemFont(ofSize: 14.0)
        label.numberOfLines = 0
        annotationView.detailCalloutAccessoryView = label

        label.widthAnchor.constraint(lessThanOrEqualToConstant: label.frame.width).isActive = true
        label.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
    }
    return annotationView
}

if / else循环描述了标题和子文本,在点击右侧的按钮时显示。

现在的问题是:当我在if部分中写入“ print(annot.subtext)”时,它实际上会打印我分配给此特殊注释的子文本。现在,我的目标是将其编写为实际的子文本,该文本在点击右侧的注释按钮时出现。所以我在else-part里面写了“ label.text = annot.subtext”,但是什么也没发生。

我尝试调试并编写了“ label.text =” any text“”,点击按钮时它实际上显示了“ any text”作为注释的子文本。 之后,我将“ print(annot.subtext)”放入else部分,就在“ label.text =” any text“”之前,但是没有打印出来。相反,它显示了一个错误:“ CUICatalog提供了无效的资产名称:'',同时仍在注释标注中写入了“任何文本”

我想知道,因为在互联网上查找此错误代码时,图像或视频始终是某种文件问题,但是这一次,当我将与if部分相同的代码放到其他部分。

也许你们中有些人有个主意!

非常感谢

Gus

0 个答案:

没有答案
相关问题