07-21-2023, 10:30 PM
Taking answers from here and there, I came up with this neat solution.
Create a parent view controller with this function.
```
class ParentViewController: UIViewController {
func convert<T: ParentViewController>(to _: T.Type) {
object_setClass(self, T.self)
}
}
```
This allows the compiler to ensure that the child view controller inherits from the parent view controller.
Then whenever you want to segue to this controller using a sub class you can do:
```
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let parentViewController = segue.destination as? ParentViewController {
ParentViewController.convert(to: ChildViewController.self)
}
}
```
The cool part is that you can add a storyboard reference to itself, and then keep calling the "next" child view controller.
Create a parent view controller with this function.
```
class ParentViewController: UIViewController {
func convert<T: ParentViewController>(to _: T.Type) {
object_setClass(self, T.self)
}
}
```
This allows the compiler to ensure that the child view controller inherits from the parent view controller.
Then whenever you want to segue to this controller using a sub class you can do:
```
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let parentViewController = segue.destination as? ParentViewController {
ParentViewController.convert(to: ChildViewController.self)
}
}
```
The cool part is that you can add a storyboard reference to itself, and then keep calling the "next" child view controller.