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?
First, IBOutlet IBOutlet is a keyword which is added to a variable declaration. It's an indicator. It does not affect the declaration in any way. However, when the Interface Builder sees it, it will allows a programmer to set this variable through the “outlet” mechanism inside Interface Builder.
If you have a property defined that you want to make accessible to your storyboards, just add the @IBOutlet attribute before your property. Similarly with @IBAction to connect storyboard actions back to code. class MyViewController: UIViewController { @IBOutlet weak var likeButton: UIButton?
The official answer from Apple is that IBOutlets should be strong. The only case when an IBOutlet should be weak is to avoid a retain cycle. A strong reference cycle can result in memory leaks and app crashes.
By doing so you are re-declaring
the label
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
and setter
of a properties
in subclasses to provide extra validations or functionalities. Refer to the Swift guide: override getter 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 different viewControllers
of your SuperClass
from story board if you give Subclasses
names in storyboard to different view Controllers.
Just define
class SuperClass{
@IBOutlet weak var label: UILabel! = nil
}
SubClass1
repersent view controller1
in storyboard derived from SuperClass
SubClass2
repersent another view controller2
in storyboard derived from SuperClass
Than Go to Assistant Editor
and open SuperClass
one side and other side view controller1
and connect outlet from SuperClass
to label
in storyBoard in view controller1
.Drag from SuperClass
label
to storyBoard in view controller1
Now again open SuperClass
one side and other side view controller2
and connect outlet from SuperClass
to label
in storyBoard in view controller2
.Drag from SuperClass
label
to storyBoard in view controller2
If you click on SuperClass
outlet than you will see two labels conneted to different viewControllers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With