My top level UIView
contains a some labels and a drawing canvas. The drawing canvas is itself a UIView
which overrides its drawRect()
method and, of course, the various touch methods. When the user has finished drawing, I want to update one or more labels that are outside the canvas (i.e. are all components of the top level UIView
).
What's the best way to do it?
I currently have the top level UIViewController
pass the various labels into my CanvasView
s from the top level viewDidLoad()
method and I store them locally. Whenever I need to update a label, I do so from the local copies of these sibling views I have - But I'm not happy with this because it feels kludgy.
To explain further, this is what I do in my top level UIViewController
:
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
for v in self.view.subviews {
if v .isKindOfClass(CanvasView) {
let canvasView = v as CanvasView
canvasView.setMyLabel(myLabel)
}
}
}
And then in my CanvasView.swift
file, I have:
private var myLabel: UILabel!
...
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
// do stuff ...
setNeedsDisplay()
myLabel.text = "etc.etc."
}
Is there a better way?
Thanks.
Ave