Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 207
PMIW Avatar asked Aug 24 '14 16:08

PMIW


2 Answers

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()
    }

}
like image 72
rilar Avatar answered Nov 16 '22 00:11

rilar


i think a cleaner solution is to use delegation:

in the ParentViewController

class ParentViewController: UIViewController ,ContainerDelegateProtocol
{
@IBOutlet weak var containerView: UIView!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    //check here for the right segue by name
    (segue.destinationViewController as ContainerViewController).delegate = self;
}
func Close() {
        containerView.hidden = true;
    }

in the ContainerViewController

protocol ContainerDelegateProtocol
{
    func Close()
}
class ContainerViewController: UIViewController {


    var delegate:AddTaskDelegateProtocol?

    @IBAction func Close(sender: AnyObject) { //connect this to the button
        delegate?.CloseThisShit()

    }
like image 33
dowi Avatar answered Nov 16 '22 00:11

dowi