0

I have two views, A and B, both inheriting from C.

In storyboard, I define a UIViewController as type C.

Another view, lets say D, has two segues to C.

I want to be able to cast the destination according to the relevant segue:

if segue.identifier = "ToA"
{
   // Load view A
}
if segue.identifier = "ToB"
{
   // Load view B
}

I have tried it and casting fails. Is it possible?

I found another thread here on SO that says it is not, yet there was a reply there saying it is possible. How can I make this casting work?

Or should I just fuse A and B together? I really don't want to do that.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ofri
  • 289
  • 5
  • 17

1 Answers1

1

This is not possible, since inheritance does not work like this. A and B are also C. But C is neither A nor B. And C is what is initialized and pushed/presented/embedded when you perform your segues.

You need to create two different ViewControllers A and B in your storyboard. They can have some IBOutlet/IBAction connections to your C.swift file.

Lausbert
  • 1,471
  • 2
  • 17
  • 23
  • Yeah that's what I thought, but is there no way to use only one ViewController? Just to minimalize the objects in storyboard – Ofri Dec 08 '18 at 13:09
  • It depends on your setup. What is actually shared between A and B? All of your UI but different logic? You could perform segues to A and B, which embed C as a childviewcontroller. The different logic could then be added via delegate pattern. But as is said it depends on you setup. Maybe you can add more details to your question. – Lausbert Dec 08 '18 at 22:07
  • Can you also take a look here please ? https://stackoverflow.com/questions/53681003/change-same-uicollectionviewcell-layout-for-different-views – Ofri Dec 09 '18 at 04:15