Search code examples
iosswiftuiviewuibuttonuicontainerview

Hide a view container with a button in the ViewContainer


I have a View. In this view, I have a Container View. And in the ContainerView I have a button.

When I am touching the button of the ContainerView, I want the ContainerView become hidden.

I want to do something like that :

class ContainerView: UIViewController {

    @IBAction func closeContainerViewButton(sender: AnyObject) {
        //I try this : self.hidden = false
        //or this :    self.setVisibility(self.INVISIBLE)
    }

}

Any idea how do it?


Solution

  • There are serval ways but here is the easiest one, not prettiest though. You should really use delegates but this is a hacky way to get started. Just create a global variable of the class that holds the container (startController in this case). Then call it from your other view controller (MyViewInsideContainer) and tell it to hide the view you´re in. I have not run this code but it should work.

    var startController = StartController()
    
    class StartController:UIViewController {
    
        @IBOutlet var myViewInsideContainerView: UIView
    
        ....
    
        override func viewDidLoad() {
            super.viewDidLoad()
            startController = self
        }
    
        func hideContainerView(){
            self.myContainerView.hidden = true
        }
    }
    
    class MyViewInsideContainer:UIViewController {
    
        ...
    
        @IBAction func hideThisView(sender: AnyObject) {
            startController.hideContainerView()
        }
    
    }