Access container view child properties swift

container view swift
container view controller multiple views
swift container view programmatically
remove all child view controllers swift
ios container view controller example swift
switching view controllers with uisegmentedcontrol swift
access parent view controller from child swift 3
page view controller in container view swift

What I want to achieve:

User presses the button in the ViewController then, the color of the button placed in the container view should change its color to red.

How can I get access of the button placed in the container view, from the ViewController?

Step by step:

  1. Name the segue between your view controller and container view controller.
  2. Add a property to your view controller which will contain the container view controller.
  3. In your view controller implement a method prepareForSegue(_:sender:).
  4. In the method check if segue.identifier equals the identifier you specified in step 1.
  5. If true, then save the segue.destinationViewController to your property from step 2.
  6. Now you have the container view controller stored in your property so you can do customization from your class. You should have the view controller stored in viewDidLoad() method already.

Example:

var containerViewController: YourContainerViewControllerClass?
let containerSegueName = "testSegue"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == containerSegueName {
        containerViewController = segue.destinationViewController as? YourContainerViewControllerClass
    }
}

iOS Swift tip: Getting references to container child view controllers, Container Views allow us to add child view controllers via Storyboard. Child view controllers are convenient as they allow us to break up a  iOS Swift tip: Getting references to container child view controllers. Container Views allow us to add child view controllers via Storyboard. Child view controllers are convenient as they allow us to break up a complicated view and view controller in to separate, reusable views, each with their own view controller.

I recommend not to rely on segue.identifier, but rather test for destination type directly:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    if let vc = segue.destination as? YourViewController {
        vc.someVariable = true
    }
}

This way you avoid mistakes with a misspelled segue name.

Managing View Controllers With Container View Controllers, The view of the child view controller becomes part of the parent view Start by renaming ViewController.swift to MasterViewController.swift. I prefer to use lazy properties because it instantiates the child view In other words, the container view controller should only access the child view controller's view  Remember that a container view controller is responsible for sizing and positioning the view of the child view controller it manages. The child view controller is the only view controller in charge of managing the views of its view hierarchy. In other words, the container view controller should only access the child view controller's view.

Swift 4, Xcode 9.4.1
var contentViewController : UIContentViewController?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == containerSegueName {
       contentViewController = segue.destination as? UIContentViewController
    }
}

Container View Controllers, Massive View Controllers are a common pain with iOS development. Both child views controllers embedded in container views The childViewControllers property of UIViewController is a read-only array of child view controllers. the two child view controllers from the Storyboard when first accessed  A container should access only the root view of each child—that is, the view returned by the child’s view property. It should never access any of the child’s other views. It should never access any of the child’s other views.

Swift 3 for macOS:

// MARK: - Container View Controller

var containerViewController: ContainerViewController?

let containerSegueIdentifier = "Container Segue"

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if segue.identifier == containerSegueIdentifier {
        if let connectContainerViewController = segue.destinationController as? FormationViewController {
            formationViewController = connectContainerViewController
        }
    }
}

Check identifier and controller class.

View Controller Programming Guide for iOS: Implementing a , A container should access only the root view of each child—that is, the view returned by the  You can access these child view controllers via the childViewControllers property or there is a method you can override prepareForSegue:sender: and capture the destination view controllers of the segue about to be called. This is also a good point to pass properties to the child view controllers if any are needed.

parent, If the recipient is a child of a container view controller, this property holds the view Prior to iOS 5.0, if a view did not have a parent view controller and was being presentingViewController property to access the presenting view controller. //I'm trying something like this, but is not working let theSubviews : Array = container_view.subviews for (view : NSView) in theSubviews { view.removeFromSuperview(container_view) } What I am missing? UPDATE. My app has a main container_view. I have to add different other views as subviews to container_view in order to provide a sort of

How to Pass Data to an iOS Container View, After prepareForSegue has been called, you can optionally store the reference to the child view controller in a property on your view controller. Change the alpha property of the view in interface builder from 1 to 0. This will show container view A at startup and hide B. You may be wondering if you could solve this problem by dragging an embed segue to connect the container view with the second child view controller. That way, you would have one container view with two child view

Implementing a Container View Controller, controllers are most often used to facilitate navigation and to create new user interface types based on existing content. Want to get access to a child component, directive or a DOM element from a parent component class? It’s easy to do with the ViewChild decorator. ViewChild returns the first element that matches a given component, directive or template reference selector. In cases where you’d want to access multiple children, you’d use ViewChildren instead.

Comments
  • Thanks very much for your detailed answer but where can I lets say edit the MyButton.backgroundColor = UiColor.blackColor() ? and how will I add an override function in the action of the button placed in my viewcontroller?
  • containerViewController.MyButton.backgroundColor.. in your view controller (not the container view controller)
  • Yep that worked for me thanks a lot for the detailed answer!!
  • Awesome! Thank you so much!
  • Need to use override func prepare(for segue: UIStoryboardSegue, sender: Any?) { for swift 3 & Xcode 8.3 otherwise this method won't be called
  • This should be the accepted answer. If you have more then one UIContainerView and try the .identifier approach you will fail :D thanks @andrey