ios 在 ViewContainer 中隐藏带有按钮的视图容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25473913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide a view container with a button in the ViewContainer
提问by PMIW
I have a View
. In this view, I have a Container View
. And in the ContainerView
I have a button.
我有一个View
. 在这种情况下,我有一个Container View
. 在ContainerView
我有一个按钮。
When I am touching the button of the ContainerView, I want the ContainerView become hidden.
当我触摸 ContainerView 的按钮时,我希望 ContainerView 被隐藏。
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?
知道怎么做吗?
回答by rilar
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.
有几种方式,但这里是最简单的一种,虽然不是最漂亮的。您确实应该使用委托,但这是一种入门方法。只需创建一个包含容器的类的全局变量(在本例中为 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()
}
}
回答by 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
在 ContainerViewController 中
protocol ContainerDelegateProtocol
{
func Close()
}
class ContainerViewController: UIViewController {
var delegate:AddTaskDelegateProtocol?
@IBAction func Close(sender: AnyObject) { //connect this to the button
delegate?.CloseThisShit()
}