Reputation: 385
I was following a Swift tutorial where we wrote a subclass of UIImageView called DragImg and within that subclass overrode some functions, namely touchesBegan, touchesMoved and touchesEnded.
I then went into the identity inspector for the images I wanted to apply the subclass to and changed the class to DragImg and then hooked them up to ViewController.swift as an outlet.
However, in my ViewController.swift file I declared them as a UIImageView object i.e.
@IBOutlet weak var HeartImg: UIImageView!
instead of
@IBOutlet weak var HeartImg: DragImg!
Even though I didn't change this, the image still functioned as per the functions definitions that we wrote in the subclass.
I am not sure why this still worked as I thought superclasses couldn't access functions of subclasses. Does that mean that the declaration above is largely redundant and the only thing that made a difference was changing the class in the identity inspector?
Could someone explain this please?
Upvotes: 0
Views: 114
Reputation: 23701
When you create a subclass you are defining an "is-a" relationship. Your subclass, DragImg
is a (or I should say is-a) UIImageView. You should be able to refer to it as a UIImageView anywhere in your code because it is one.
Having your outlet refer to the class' superclass is perfectly ok.
Upvotes: 0
Reputation: 5797
That is correct because you used DragImg in the identify Inspector which is really the class used when the instance is created. Even if you used UIImageView in your UIViewController, it is still as DragImg object instance. If you want to have the UIImageView then change the identify Inspector back to UIImageView.
Upvotes: 2