简体   繁体   中英

Override IBOutlet properties from super class?

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?

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.

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"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM