12

I would like to create a super class in a framework that modifies IBOutlet properties. However, I would like a subclass to be connected to the storyboard, since I don't want to connect the controls to the class in the framework.

For example, the super class in my framework looks like this:

public class MySuperDetailViewController: UIViewController {

    @IBOutlet public weak var titleLabel: UILabel?
    @IBOutlet public weak var dateLabel: UILabel?
    @IBOutlet public weak var contentWebView: UIWebView?

    ...
}

Then in the subclass, I would like to control-drag controls onto the subclass. So I have to expose those properties by overriding. I'm trying to do this but it won't allow me:

class MyDetailViewController: MySuperDetailViewController {

    @IBOutlet weak var titleLabel: UILabel?
    @IBOutlet weak var dateLabel: UILabel?
    @IBOutlet weak var contentWebView: UIWebView?

}

The error I get is: Cannot override with a stored property 'titleLabel', 'dateLabel', and 'contentWebView'.

How can I do this or better approach to this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
TruMan1
  • 33,665
  • 59
  • 184
  • 335

2 Answers2

15

Don't try to recreate the variables in the subclass; the IBOutlet variables still exist. You can still connect them inside of Interface Builder in a number of ways.

  1. Utilites (right panel) -> Connection Inspector - drag from the list of IBOutlets.
  2. Document Outline (left panel) - drag from MyDetailViewController
  3. Drag from the yellow circle when you have the UIViewController selected in Interface Builder

Note: All UIViewController subclasses inherit an IBOutlet named view; this property already exists in Interface Builder even though you can't click + drag to connect it.

Kevin
  • 16,696
  • 7
  • 51
  • 68
0

All sub-classes get direct access to the outlets in the super-class. So you can just change the properties of the outlets direcly in the respective sub-class. Like this:

class MySubClass: MySuperClass {
    override func viewDidLoad() {
        super.viewDidLoad()
        mySuperClassOutlet.text = "This overrides superclass text"
    }
}
Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18