3

If I have a storyboard that contains a view controller named "ABCViewController"

"A_ViewController" is a subclass of "ABCViewController"

is there a way to initiate the view controller "ABCViewController" from the storyboard as "A_ViewController" ?

Mohammed
  • 1,432
  • 4
  • 18
  • 34

6 Answers6

6

Here is what I do. Hope it helps.

let abcController =  storyboard.instantiateViewControllerWithIdentifier("ABCViewController") as ABCViewController

object_setClass(abcController, A_ViewController.self) 

now you can cast abcController to specific child view controller if needed.

minhazur
  • 4,928
  • 3
  • 25
  • 27
4

You can't do this with storyboard. instantiateViewControllerWithIdentifier will return instance of ABCViewController, you can cast it to superclass (i.e.UIViewController) but casting to subclass (A_ViewController) won't work. You can read why you can't do this here.

If you want to have two view controllers with same layout, but different classes you should use xib

Community
  • 1
  • 1
Pr0Ger
  • 528
  • 6
  • 15
1

I do not think it is possible to achieve what you are trying to do, reason being:

In Storyboard you give specify the concrete class name in IB for a particular UIViewCotnroller UI. This means when you you instantiate this UIViewController that specific class will be created.

In your case Base class is specified in IB and you are trying to downcast it a derived class which is bound to fail.

Abdullah
  • 7,143
  • 6
  • 25
  • 41
0

With a reference to the storyboard, you should be able to do a regular cast to a base type:

let myController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as UIViewController

Remember to set the identifier for the view controller in the storyboard too!

gregheo
  • 4,182
  • 2
  • 23
  • 22
0

Not sure what you are asking, presuming one of the following :

1) You want to link the storyboard viewcontroller to the A_ViewController subclass of ABC_ViewController. You can simply do this by selecting Class from the identity inspector :

enter image description here


2) You already have the ViewController linked to ABCViewController from the storyboard IndentityInspector and you want to programatically fetch it as A_ViewController type (subclass).
You cannot downcast (from the parent to the child), so you cannot do something like (presume that sbID1 is the StoryBoard ID corresponding to your view controller) :

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("sbID1") as A_ViewController

The reason is that, generally speaking from an inheritance POV, you cannot pass a parent type as a child type (only the other way around). You can downcast from Parent to Child only when you know that you have initially instantiated the object as a Child.

Sorin J
  • 542
  • 1
  • 4
  • 14
0

I have tried different ways but not able to solve it. I used following way to tackle it : enter image description here

  1. In storyboard, create copy of VC with child 2
  2. Bind VC identifier to storyboard
  3. Implement all IBOutlet and action in Base only
  4. Instantiate Child 1 and child 2 from any class as required.

Benefits:

  1. This way we can keep the sam UI for 2 different VC.
  2. we can implement all UI related functionality in Base
  3. We can reuse common code use BaseVC
  4. We can implement logic in respective VC i.e. child 1 and child 2
RohitK
  • 1,444
  • 1
  • 14
  • 37
  • Technically, not the same UI for the 2 VCs, you have to separate copies, You have to changes both copies in case of updates (or delete & reclone the second copy) – Haroun SMIDA Jul 08 '21 at 16:00