iosswiftxcodeuiviewcontrolleruicontainerview

How to reuse same view controller in multiple container view in swift?


I have a container view in a view controller (lets say A) with an embed segue to a view controller (lets says B) which is in the same storyboard. Now the requirement is I have to use this same embedded view controller B in another container view in view controller C which is in different story board. Is it possible to reuse view controller B in multiple container views ?


Solution

  • 1) For view controller B, add a storyboard identifier

    enter image description here

    2) Add a container view inside view controller C enter image description here

    3) Delete the view controller that is embedded to View Controller C

    4) Add a "Storyboard Reference" next to view controller C, then drag from the container view inside view controller C to the storyboard reference and create an "Embed" segue

    enter image description here

    5) Select the storyboard reference and set the "Reference ID" to match view controller B's identifier. Also make sure "Storyboard" has the name of the storyboard that view controller B is in.

    enter image description here