1

I want to make a ViewController base-class which I can reuse throughout the project.

I want to create a pop-up ViewController which I can adjust with multiple sub-classes which all share the same basic layout (inherited from the base-class). I would like the layout of the base class to be defined in a storyboard scene in an attempt to follow apple's guidelines (not using xib's). This also includes setting up all constraints in interface builder, and not in code.

All I want to do is the right thing :)

My problem is that if I start to subclass my ParentViewController (which has an associated scene in a Storyboard), the app won't let me show the ViewController. If I instantiate through the Storyboard ID, I can't cast it to my subclass. If I instantiate by creating an instance of the subclass-ViewController, it won't show, as the UI in the storyboard file is "locked" to the ParentViewController.

How do I make a base-ViewController with an associated scene in a storyboard file, which I can use various sub-classes (or the like). To be concrete: I want to make a pop-up, which can vary slightly depending on the usage. I don't want to make init-methods for each variation, as that would defeat the purpose of attempting to split code.

Thanks for any help or comment!

NooberMan
  • 101
  • 1
  • 8
  • Does this answer your question? [Reuse parent ViewController storyboard file in Child ViewController](https://stackoverflow.com/questions/50049509/reuse-parent-viewcontroller-storyboard-file-in-child-viewcontroller) – Manikandan Nov 24 '19 at 15:09

1 Answers1

0

object_setClass(Sets the class of an object.) will override instance of AViewController with BViewController Class. So on top of AViewController you can add some more methods.

when you have similar viewcontroller with small changes. you have to create different viewcontrollers. using this method you can create on basic viewcontroller with storyboard and reuse that viewcontroller .

 class BViewController{
       static func vcInstanceFromStoryboard() ->BViewController? {
            let storyboard = UIStoryboard(name: "AViewController"), bundle: UIBundle.main)
            let instance = storyboard.instantiateInitialViewController() as? AViewController
            object_setClass(instance, BViewController.self) // 
            return (instance as? BViewController)!
        }
    .....
    } 

This is an example of how do we use it:
let vc = BViewController.vcInstanceFromStoryboard()
self.present(vc , animation : true)
Manikandan
  • 1,195
  • 8
  • 26