Hide a view container with a button in the ViewContainer(用 ViewContainer 中的按钮隐藏视图容器)
问题描述
我有一个视图.在这个视图中,我有一个 Container View.在 ContainerView 我有一个按钮.
I have a View. In this view, I have a Container View. And in the ContainerView I have a button.
当我触摸 ContainerView 的按钮时,我希望 ContainerView 被隐藏.
When I am touching the button of the ContainerView, I want the ContainerView become hidden.
我想做这样的事情:
class ContainerView: UIViewController {
    @IBAction func closeContainerViewButton(sender: AnyObject) {
        //I try this : self.hidden = false
        //or this :    self.setVisibility(self.INVISIBLE)
    }
}
知道怎么做吗?
推荐答案
有多种方法,但这里是最简单的一种,虽然不是最漂亮的.你真的应该使用委托,但这是一种入门的方式.只需创建一个包含容器的类的全局变量(在本例中为 startController).然后从您的其他视图控制器 (MyViewInsideContainer) 调用它并告诉它隐藏您所在的视图.我没有运行此代码,但它应该可以工作.
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()
    }
}
                        这篇关于用 ViewContainer 中的按钮隐藏视图容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用 ViewContainer 中的按钮隐藏视图容器
				
        
 
            
        基础教程推荐
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				