Reputation: 2293
I have a custom class for a map pin callout in Swift app, which connects to a .xib file. I am trying to change to text of a label inside of the view when the user clicks on the pin an the view loads. Here is what I have I have:
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)
}
}
I keep getting the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
With this line being highlighted:
testLabel.text = "test"
How do I get this to work? How can I change the text of an outlet when this callout view loads?
Upvotes: 0
Views: 194
Reputation: 4337
You can reference to it: CustomView You can create customview with list steps below:
File's Owner
to this class.contentView
, drag all element you want to class too.And in class add this code:
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)
}
Now and you can use customView. You can check my demo: Demo
Upvotes: 1