Hide a view container with a button in the ViewContainer

邮差的信 提交于 2019-12-03 07:26:13

问题


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?


回答1:


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

}



回答2:


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

    }


来源:https://stackoverflow.com/questions/25473913/hide-a-view-container-with-a-button-in-the-viewcontainer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!