0

I have a ViewController (BViewController) that's inheriting from another UIViewController Subclass (AViewController). (The reason I want to do this is I'm reusing the same view in storyboard 3+ times for different screens.)

When I call:

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "AViewController") as! BViewController
        self.show(vc, sender: self)

I get this error:

Could not cast value of type 'test.AViewController' (0x10d08b478) to 'test.BViewController' (0x10d08b3f0).

Here are my subclasses, they have nothing in them.

class AViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

-

class BViewController: AViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

The class in Storyboard is set to AViewController because I'm trying to share IBOutlets across all children without recreating the views. There is only one View Controller Scene in my UIStoryboard.

aroooo
  • 3,778
  • 3
  • 32
  • 63
  • 2
    You cannot cast your view controller to `BViewController` (subclass of `AViewController` or not) if you set the class to `AViewController` in the storyboard. – albertamg Nov 23 '16 at 09:39
  • 2
    B is A, but A isn't B, meaning you can cast B to A, but not A to B. – Mercurial Nov 23 '16 at 09:42
  • I unset the class and now I get: Could not cast value of type 'UIViewController' (0x10260c718) to 'test.BViewController' (0x1009bf3f0). – aroooo Nov 23 '16 at 09:42
  • @Mercurial does this mean there isn't really a way to share the storyboard views from one parent? If I don't set the UIViewController class then I can't access the shared IBOutlets that will be common to all subclasses – aroooo Nov 23 '16 at 09:43
  • @arooo I don't think so. Storyboards are used to project view controller flows. If you need reusability, move your view to a nib and set your ViewController as a file owner. That way you'll be able to use it with multiple controllers. – Mercurial Nov 23 '16 at 10:34

3 Answers3

1

According to the answer in this thread, it isn't possible to reuse a single UIViewController Scene with multiple subclasses with UIStoryBoard. It is however possible with nib files.

How to use single storyboard uiviewcontroller for multiple subclass

Community
  • 1
  • 1
aroooo
  • 3,778
  • 3
  • 32
  • 63
0

You probably don't put the right view controller identifier:

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "BViewController") as! BViewController
self.show(vc, sender: self)

(BViewController instead of AViewController)

EDIT: Here's an example: I have a SignupVC view controller in my storyboard, but its storyboard ID is "signup_vc"

enter image description here

Marie Dm
  • 2,447
  • 2
  • 18
  • 35
  • The identifier is AViewController because that's the parent– all subsequent subclasses that subclass AViewController (CViewController, DViewController) use the single UIViewController with the identifier AViewController to share the IBOutlets – aroooo Nov 23 '16 at 09:41
  • Still, I think you should put BViewController as storyboard identifier on your second view controller and AViewController on your first. – Marie Dm Nov 23 '16 at 09:44
  • They can have different storyboard IDs, even if one is the parent of this other. The storyboard ID (that you put in your storyboard) is not the class name. – Marie Dm Nov 23 '16 at 09:44
  • What would I put as the identifier for a CViewController that shares the same ViewController in Storyboard? – aroooo Nov 23 '16 at 09:45
  • Your "AViewController" is the class name, but its storyboard ID can be "a_vc" for example. It's the same for your other vcs. – Marie Dm Nov 23 '16 at 09:47
  • But I only have one View Controller Scene– I'm trying to reuse the Storyboard Scene (to share the IBOutlets)- I'm deliberately trying to avoid creating multiple Scenes. – aroooo Nov 23 '16 at 09:50
  • Ok I see. Unfortunately I don't think you can do it that way. You should do the opposite, meaning 3 vcs on your storyboard and one class AViewController they all inherit. – Marie Dm Nov 23 '16 at 09:57
  • Yeah– I was hoping to avoid that; it's a bunch of very similar views with just string an image changes. – aroooo Nov 23 '16 at 09:59
  • Sorry, I don't know other way :) – Marie Dm Nov 23 '16 at 10:01
0

You have to set your view controller's class to BViewController in your storyboard

kemkriszt
  • 280
  • 1
  • 16