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

PMI*_*MIW 12 uibutton uiview ios uicontainerview swift

我有一个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)
    }

}
Run Code Online (Sandbox Code Playgroud)

知道怎么做?

ril*_*lar 20

有仆人的方式,但这里是最容易的,但不是最漂亮的.你应该真的使用委托,但这是一个开始的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()
    }

}
Run Code Online (Sandbox Code Playgroud)


dow*_*owi 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;
    }
Run Code Online (Sandbox Code Playgroud)

在ContainerViewController中

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


    var delegate:AddTaskDelegateProtocol?

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

    }
Run Code Online (Sandbox Code Playgroud)