Cesare Cesare - 6 months ago 32
Swift Question

How to connect an UIView to my custom class

I connected a view which belongs to a view controller to a custom class via an

IBOutlet
declaration.

Then, in the
viewDidLoad
method I declare a variable and I use the variable to connect
myView
with the class
myClass
.

var myConnectedView = myView as! myClass


Though, this code returns a runtime error:
Could not cast value of type 'UIView'
. I seem to find no way to assign a
UIView
to a class.

Question

How do I assign a
UIView
to a
UIView
class?

Answer

In the Storyboard interface you can set the class of the UIView. If you head over into the Utilities panel you can find an option where you can modify the class of the UIView.

Right now it says UIView (this is default). You have to set it there to myClass if this is a class that inherits from a UIView.

Right now the view is assigned to the UIView class.

Comments