Reputation: 48
I'm sorry in advance for this basic question, I'm just having a hard time finding the answer. I have a view class that subclasses UIView, and has a few basic methods, draw() obviously and others. In my Main.storyboard I've added a UIView element and set its class attribute to this view class I've created. The class is like:
class KeyboardView: UIView {
// local variables ...
override function draw(_ rect: CGRect) {
...
}
class changeKord() {
...
}
// other methods ...
}
From my main ViewController class I would like to reference the instance that is actually created at runtime. When I create an outlet in code for this UIView I need to instantiate it, and I can't access the instance methods in the class. Is there some way to get the actual instance that's created from the outlet variable? Or am I missing something?
What I did was create a global variable outside the View Controller class of type KeyboardView, and in the draw method in the KeyboardView subclass I set that global variable to self, which allows me to reference the instance directly. Which works but seems really hacky.. Is there a way to get the instance from the IBOutlet reference by chance?
Upvotes: 1
Views: 996
Reputation: 19750
I've just put this quick example together, Notice in the screenshot that the IBOutlet has a type of CustomView.
What I done was to add the UIView in the storyboard and then change the class in the property inspector to CustomView and THEN created the IBOutlet. This then used the correct type, so I could reference the custom properties and functions of the view.
You can update the type in your outlet if you created it before as long as it matches up with the class in the property inspector.
Upvotes: 1