Question

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?

Was it helpful?

Solution

Just add the IBOutlet modifier in the superclass.

OTHER TIPS

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 viewControllersof 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

enter image description here

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

Agreed that the short answer is simply:

"Just add the @IBOutlet modifier in the superclass."

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 the public attribute to the variable definition. EDIT: instead, use open 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:

public class CustomViewController: UIViewController {

    override public func viewDidLoad() {
        super.viewDidLoad()
    }

@IBOutlet public var myBackgroundView: UIView!
@IBOutlet public weak var myLabel: UILabel!

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

import UIKit
import mymodule

class SubclassedCustomViewController: CustomViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        myBackgroundView.backgroundColor = UIColor.blueColor()
        myLabel.text = "My special subclass text"
    }

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:

  • Make the IBOutlet connections in the superclass from the superclass' XIB files
  • Make the IBOutlet connections in the subclass from the subclass' XIB files, with the same IBOutlet property names as in the superclass for the ones you need to inherit.
  • Delete the declaration of the IBOutlet variables in the subclass
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top