All descendants of my specific class are to have a UILabel
instance variable. So in my parent class I have var label: UILabel
. I want to have it in the sublclass as well, but as an IBOutlet
. How do I do this?
I added the IBOutlet
of the same name, and added weak to both variable declarations. But I get an error about "Cannot override with a stored property".
How should I be doing this? And is it possible to not have to instantiate the superclass' version as I just want it for subclassing?
By doing so you are
re-declaring
thelabel
property in a subclass.IBOutlet
is just a compiler notion for working with the Interface Builder.In Swift stored properties can not be overridden or redeclared in a subclass. They can only be inherited.
However you can override the
getter
andsetter
of aproperties
in subclasses to provide extra validations or functionalities. Refer to the Swift guide: overridegetter setter
You need to declare your property in superClass with
IBOutlet
.Or you can make a different property in your subclass. As also there is no meaning if you are connecting your property in one of subclasses(super class may have other) and you are not providing this implementation to other subclasses of your superclass.
EDIT: You can also set
label
outlet to two differentviewControllers
of yourSuperClass
from story board if you giveSubclasses
names in storyboard to different view Controllers.Just define
SubClass1
repersentview controller1
in storyboard derived fromSuperClass
SubClass2
repersent anotherview controller2
in storyboard derived fromSuperClass
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
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 viewControllersJust add the
IBOutlet
modifier in the superclass.Agreed that the short answer is simply:
One note though: if you will be exposing the ViewController in which the
@IBOutlet
is defined via a framework (with Carthage), you will need to add thepublic
attribute to the variable definition. EDIT: instead, useopen
access keyword`.Let's say you have a view controller CustomViewController subclassing UITableViewController, which you will build in the Carthage-delivered framework. Everything which should be accessible to the consumer application should be tagged
public
:The nice thing about this approach (and I'm totally on-board with this use case) is that you can do the IBOutlet mapping in Interface Builder on the superclass, then switch the class of the Scene to the subclass, while retaining all of the mappings.
E.g.: SubclassedCustomViewController
I realize that this question was posted a while ago, but since I just struggled with the same issue and finally came up with a solution, I figured I would still post my findings...
I understand the problem to be as follows (at least that's the one I solved):
How to have class A inherit from class B, with each class having its own XIB file with the some common IBOutlet properties? The goal being to be able to have the super class handle the actions related to the IBOutlets that are common to its subclass(es), while still being able to use Interface Builder to design the interface for the subclass(es).*
In order to do so: