Container View Controllers in Swift

If you are using a ‘Container View’ in Xcode’s storyboards it’s likely that you want to get a handle to the view controller you create inside the container view. Surprisingly, there isn’t an obvious way to do this. However, you can use the ’embed segue’ to find the handle to the container views because when the container view is added the embed segue is called. So when you create the container view give the embed segue a unique identifier and then you can grab the handle to your view controller in the prepareForSegue method as follows…

var myController: MyController?
var myOtherController: OtherController?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    guard let ident = segue.identifier else {
    switch ident {
    case "MyEmbedSegueIdentifier":
        myController = segue.destinationViewController as? MyController
    case "MyOtherEmbedSegueIdentifier":
        myOtherController = segue.destinationViewController as? OtherController
  • Yahya Najafi

    Hi Marek,

    Sorry that I am requesting this here but I could not find another way to contact you. You did a great work on iOS-File-Browser.I am wondering if I can use it in my app to choose a file before processing it.

    Thanks in advance

  • frank miller

    I have a case where im incorporating a slide menu to my app and when the users select a menu item from the slide menu it will close the menu and display the associated view. I can not get it to work.