Hide a view container with a button in the ViewContainer

别说谁变了你拦得住时间么 提交于 2019-12-02 22:16:16

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

}
dowi

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

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