LateNate LateNate - 1 year ago 91
iOS Question

Bar button action on ViewController in ViewContainer

I have a container embedded in a ViewController with a navigation bar. The ContainerView contains another ViewController with some textfields.

When the ContainerView is first displayed, the textfields are disabled.

What I would like to do is add an edit button to the navigation bar which enables the textfields.

Basically, is it possible for a bar button item in a viewcontroller to have an action on another view controller displayed in a container?

Answer Source

in this case you can do a little trick

the bar button will be in the ViewController but we can store the content of the ContainerView in a variable in the first ViewController

to do that i would suggest to make the content of the ContainerView has a custom class so you would write a function in it

here is an example on how to catch the content of the ContainerView

class ViewController: UIViewController{
    weak var containter: ContainerViewController!

    @IBAction func menuAction(_ sender: UIBarButtonItem) {
        //do what you want containter.doAction()

    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
      if let vc = segue.destination as? ContainerViewController {
         self.containter = vc
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download