0

I have a Storyboard named CustomComponents.storyboard and I had designed BaseViewController in Storyboard. For example, I set backgroundColor of BaseViewController to .green in Interface Builder. I inherit RootViewController from BaseViewController. I expect the backgroundColor of RootViewController to be .green.

This is my RootViewController

class RootViewController: BaseViewController {

}

and this is my BaseViewController

class BaseViewController: UIViewController {

}

both the classes are empty and I have just set the backgroundColor in IB.

But, I see white background when I run the app. But, when the color is set programmatically in BaseViewController, green background colour is applied to RootViewController as expected.

What am I missing to inherit BaseViewController which is designed in Storyboard? Basically, I would like to design my BaseViewController in Storyboard. What am I doing wrong?

iOS
  • 3,526
  • 3
  • 37
  • 82
  • how do you get the reference of BaseViewController?? – m1sh0 Mar 09 '20 at 11:36
  • Both are empty classes. I just inherit from `BaseViewController` – iOS Mar 09 '20 at 11:42
  • You need to instantiate the ViewController from the storyboard, or there is no way to see the changes from it. Imagine the Stroryboard as an instance of your BaseViewController, not as a definition of the class – m1sh0 Mar 09 '20 at 11:59
  • Please Check this: https://stackoverflow.com/questions/36419948/ios-swift-subclassing-viewcontroller-initialized-by-xib-nib-file && https://stackoverflow.com/questions/4418080/inherit-xib-file-in-interface-builder – Amit Mar 09 '20 at 12:16

1 Answers1

0

You can do this programmatically only if your application min spec is iOS 13, because new API available for such purpose, because storyboard does not contain a class declaration it is archive of encoded instance

So if your BaseViewController has identifier, you can instantiate RootViewController with encoded parent as

let rootController = UIStoryboard(name: "CustomComponents", 
    bundle: nil).instantiateViewController(identifier: 
                             "your_base_controller_identifier_here") { coder in
        RootViewController(coder: coder)
}

or if your controller is root

let rootController = UIStoryboard(name: "CustomComponents", 
     bundle: nil).instantiateInitialViewController { coder in
        RootViewController(coder: coder)
}
Asperi
  • 228,894
  • 20
  • 464
  • 690