0
votes

I want to subclass a view controller where the superClass and subClass are in two separate storyboards. The superClass has IBOutlets connected through it's storyboard and the same for the subClass.

Question: Is it possible to create the subclass, so that the superclass's IBOutlet connections are used from the storyboard? (AKA the IBOutlets are not nil, and awakeFromNib is called)

Setup:

SuperClass is BaseViewController using BaseStoryboard with an IBOutlet UIView property baseContainerView.

SubClass is SpecialViewController using SpecialStoryboard

SpecialViewController inherits from BaseViewController.

Current Problem:

If I instantiate an instance of SpecialViewController from it's storyboard (SpecialStoryboard), when viewDidLoad is called, the containerView property on it's superclass (BaseViewController) is nil. Is it possible to have the SpecialViewController's superclass (BaseViewController) loaded from it's storyboard as well?

1

1 Answers

2
votes

Classes support inheritance, but scenes do not.

When you subclass a view controller, it inherits any outlets that exist in its superclass, but these outlets need to be explicitly assign a value in the scene in which you use the subclass.

A scene specifies the class of its view controller, but the view controller class has no link "back" to the scenes in which it is used. There is a "many to one" relationship between scenes and view controller classes. If what you are asking were possible, how would iOS know which of the potentially many scenes in which a view controller is used you wanted to "inherit" from?

Just because the superclass defines outlet foo and you have use the superclass in some scene, it doesn't mean that foo will have some value if you use then use a subclass in a different scene (regardless of which storyboard those scenes are in). You will need to directly assign an object to foo in the scene that uses the subclass.