我在Swift应用程序中有一个自定义类用于地图引脚标注,它连接到.xib文件。我试图在用户点击视图加载的图钉时更改为视图内部标签的文本。这就是我拥有的:
import Foundation
import MapKit
class CustomCallout: UIView {
@IBOutlet weak var testLabel: UILabel!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
testLabel.text = "test"
}
override func hitTest( point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let viewPoint = superview?.convertPoint(point, toView: self) ?? point
//let isInsideView = pointInside(viewPoint, withEvent: event)
let view = super.hitTest(viewPoint, withEvent: event)
return view
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
return CGRectContainsPoint(bounds, point)
}
}
我一直收到错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
突出显示此行:
testLabel.text = "test"
我如何让它工作?如果加载此标注视图,如何更改插座的文本?
答案 0 :(得分:1)
您可以参考它:CustomView 您可以使用以下列表步骤创建自定义视图:
File's Owner
到此课程。contentView
,同时拖动您要上课的所有元素。在课堂上添加以下代码:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit() {
NSBundle.mainBundle().loadNibNamed("CutomViewDemo", owner: self, options: nil)
guard let content = contentView else { return }
content.frame = self.bounds
content.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
self.addSubview(content)
}
现在您可以使用customView。您可以查看我的演示:Demo