5

I have one base class

MyViewController: UIViewController

initialized by MyViewController.xib with some outlets. I only have set File Owner class in MyViewController.xib to MyViewController, no any init methods in MyViewController.swift (all inherited from UIViewController), and following line works just as expected:

let vc = MyViewController()

view property is set, outlets is set.

I wish to subclass MyViewController:

SecondViewController: MyViewController
{
    override init()
    {
        super.init()
    }

    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }
}

Now I expect that line

let vc = SecondViewController()

will create view controller with view and outlets inherited from MyViewController, but all outlets in vc are nil. Looks like MyViewController.xib file is now missed. What am I doing wrong?

Yury
  • 6,044
  • 3
  • 19
  • 41

1 Answers1

6

You can't extend the xib file. The SecondViewControllershould have its own xib file and its own outlets. You may define the common UI components in the base class MyViewController and for each xib you create, link the ui components directly to the base class. For example, if you have a common custom back button in all view controller, add the outlet definition in the base class and for each xib file add the UIButton and set its outlet to the base class.

Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
  • 1
    Thank you for answer. Poor to hear this. I believe that subclass system exist not for set same settings in each subclass (what if 100 subclasses with same based UI? I do not want to make 100 same xib files). Probably I don't understand inside cook of init by xib, but for now I see only one good solution - make all base UI in code in MyViewController, not in xib. – Yury Apr 05 '16 at 08:29
  • 3
    In that case yes, you can create common UI stuff manually in code in the base class OR you can create in a standalone xib file for a different UIViewController and instantiate and add it as a subview – Hossam Ghareeb Apr 05 '16 at 08:33