使用ViewContainer中的button隐藏视图容器
我有一个View
。 在这个视图中,我有一个Container View
。 在ContainerView
我有一个button。
当我触摸ContainerView的button时,我希望ContainerView隐藏起来。
我想要做这样的事情:
class ContainerView: UIViewController { @IBAction func closeContainerViewButton(sender: AnyObject) { //I try this : self.hidden = false //or this : self.setVisibility(self.INVISIBLE) } }
任何想法是如何做到的?
有喋喋不休的方式,但这是最简单的,但不是最漂亮的。 你应该真的使用代表,但这是一个开始的黑客办法。 只需创build一个持有容器的类的全局variables(本例中为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() } }
我认为一个更清洁的解决scheme是使用委托:
在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() }