使用ViewContainer中的按钮隐藏视图容器

时间:2014-08-24 16:40:06

标签: ios swift uiview uibutton uicontainerview

我有View。在此视图中,我有一个Container View。在ContainerView我有一个按钮。

当我触摸ContainerView的按钮时,我希望ContainerView变为隐藏状态。

我想做那样的事情:

class ContainerView: UIViewController {

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

}

知道怎么做?

2 个答案:

答案 0 :(得分:19)

有塞尔维亚方式,但这里最容易,但不是最漂亮的。你应该真的使用委托,但这是一个开始的hacky方式。只需创建一个包含容器的类的全局变量(在本例中为startController)。然后从你的其他视图控制器(MyViewInsideContainer)调用它并告诉它隐藏你所在的视图。我没有运行这段代码,但它应该可以工作。

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

}

答案 1 :(得分:9)

我认为更清洁的解决方案是使用委托:

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;
    }
ContainerViewController中的

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


    var delegate:AddTaskDelegateProtocol?

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

    }