title: How to implement a BaseViewController through storyboard or xib?
body: <p>Wait, I know this question title looks very duplicated, but it really is not.<br>
  I have been searching for whole day but cannot find any good solution.</p>
  
  <h3>Definition of terms</h3>
  
  <p><code>BaseViewController</code>: A class that extends <code>UIViewController</code>, responsible for some common behaviors across different subclassed ViewControllers.<br>
  <code>VC</code>: Short form of ViewController, I will use it from now on<br>
  <code>dialog</code>: In other words, popup; a VC that does not cover the whole screen, and is present modally.  </p>
  
  <h3>Scenario</h3>
  
  <p>Let me use an example to illustrate my needs.</p>
  
  <p>Let say I have an app that displays dialogs in different places.<br>
  These dialogs have something in common, including:</p>
  
  <ol>
  <li>They will blur the background when displayed</li>
  <li>They have a same width</li>
  <li>They have a same dialog container style (e.g. rounded corner radius)</li>
  </ol>
  
  <p>And they also have non common things - the content inside the dialog container.</p>
  
  <p>In order to reduce code duplication, I want to create a <code>BaseDialogVC</code> to implement the above 3 common things first, and then child class that subclass <code>BaseDialogVC</code> will provide the view inside the container.</p>
  
  <p>Therefore, I created a storyboard file which contains <code>BaseDialogVC</code> and implemented the 3 common things using Interface Builder of storyboard.<br>
  It has an empty <code>UIView</code> that acts as a container for its child class to provide their content.<br>
  And then, I created a subclass of <code>BaseDialogVC</code>, for example, <code>AddItemDialogVC</code>. This <code>AddItemDialogVC</code> is then responsible for providing its content view, and the logic of this dialog.</p>
  
  <p>Then I am stuck - I tried to instantiate from storyboard an <code>AddItemDialogVC</code>, but it will crash because storyboard does not have <code>AddItemDialogVC</code>. It only has <code>BaseDialogVC</code>.  </p>
  
  <h3>Question</h3>
  
  <p>Actually, I know one alternative that can achieve similar things.<br>
  That is - I <strong>do not subclass</strong> <code>BaseDialogVC</code>, instead I instantiate it, and add subview to it by calling an instance method of it, passing in an <code>UIView</code>.</p>
  
  <p>But I still very much want to know what's wrong with my original approach, or,<br>
  What is the best approach to this problem?</p>
  
  <p>By "the best approach", I mean it effectively reduced duplication and is easy to maintain.</p>
  
  <h3>What I have tried</h3>
  
  <p>I found <a href="https://stackoverflow.com/a/45736186/7870874">this post</a> for changing <code>BaseDialogVC</code> to <code>AddItemDialogVC</code>. But even if it successfully created an <code>AddItemDialogVC</code>, the <code>AppItemDialogVC</code> cannot init its content view (A custom View that is subclass of <code>UIView</code>, and I got an "Thread 1: EXC_BAD_ACCESS" exception.</p>
  
  <p>I think the most similar question out there is <a href="https://stackoverflow.com/questions/14111572/how-to-use-single-storyboard-uiviewcontroller-for-multiple-subclass">this thread</a>.<br>
  However, the accepted answer does not elaborate enough; while the second most voted answer is the same as <code>set_object</code> that I have tried before.</p>
  
  <p>FYI, my class for <code>AddItemDialogView</code> (NOT VC!):</p>
  
  <pre><code>class AddChoiceDialogView: UIView {
  
      @IBOutlet weak var rootView: AddChoiceDialogView!
  
      override init (frame: CGRect) {
          super.init(frame: frame)
          commonInit()
      }
  
      required init? (coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          commonInit()
      }
  
      private func commonInit () {
          Bundle.main.loadNibNamed("AddChoiceDialogView", owner: self, options: nil)
          addSubview(rootView)
          rootView.frame = self.bounds
          rootView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
      }
  
  }
  </code></pre>