I have a specific method that accepts a UIView
object, and I want to pass objects to it that can be of one of two classes. So say it accepts a UIView
that represents an animal, I want to be able to pass a DogView
and a CatView
or other animal-type classes potentially.
Within that method I want to set the nameLabel
view, which all animals have. How do I set it up so I'd be able to do this?
My first reaction was to have a super class (such as AnimalView
) that has the nameLabel
variable on it, and then subclass it for each new animal. However, if I want the nameLabel
to be an outlet, it doesn't seem settable as I couldn't have the variable in every subclass to wire the view up to in IB.
I then tried a Protocol
, but that's not polymorphic and I wouldn't be able to access the nameLabel property through a generic superclass, could I? Unlike Objective-C I couldn't ask for a UIView <ProtocolName>
and it would then allow me to ask for it.
How should I be doing this? I just want to be able to pass different kind of objects and have it be compatible with Interface Builder. Should I be approaching it completely differently?
Declare the
IBOutlet
in asuperclass
,AnimalView
. Then in Interface Builder, once you have set the customUIView's
class in the Identity inspector to beDogView
, go to the Connections Inspector and yournameLabel
will be there.You can connect outlet of
label
to differentviewControllers
with yourSuperClass
from story board if your differentviewControlelrs
in storyboard reperset bySubclasses
(derived from SuperClass) names in storyboard.1)Just define
SubClass1
repersentview controller1
in storyboard derived fromSuperClass
SubClass2
repersent anotherview controller2
in storyboard derived fromSuperClass
2)Than Go to
Assistant Editor
and openSuperClass
one side and other sideview controller1
and connect outlet fromSuperClass
tolabel
in storyBoard inview controller1
.Drag fromSuperClass
label
to storyBoard inview controller1
3)Now again open
SuperClass
one side and other sideview controller2
and connect outlet fromSuperClass
tolabel
in storyBoard inview controller2
.Drag fromSuperClass
label
to storyBoard inview controller2
If you click on
SuperClass
outlet than you will see two labels conneted to different viewControllers