我在Dock中有UIViewController
个2 UIViews
个(场景):
如何在我UIViewController
中需要的任何地方展示它?换句话说,如何从我的控制器中引用这些视图?
答案 0 :(得分:1)
在我的UIViewController
代码中,使用@IBOutlet
保留引用:
@IBOutlet weak var dockView: UIView!
答案 1 :(得分:0)
您可以将UIViews拖动到ViewController并为它们创建出口。
您想将其作为子视图添加到视图中。我连接了你在我的场景中看到的那两个按钮:
class ViewController: UIViewController {
@IBOutlet var view1: UIView!
@IBOutlet var view2: UIView!
// SHOW THE VIEWS
@IBAction func showView1(_ sender: UIButton) {
view.addSubview(view1)
view1.center = view.center
}
@IBAction func showView2(_ sender: UIButton) {
view.addSubview(view2)
view2.center = CGPoint(x: view.center.x, y: 100)
}
}
您必须从视图中删除它们。我为UIViews上的关闭按钮创建了动作:
class ViewController: UIViewController {
@IBOutlet var view1: UIView!
@IBOutlet var view2: UIView!
// SHOW THE VIEWS
@IBAction func showView1(_ sender: UIButton) {
view.addSubview(view1)
view1.center = view.center
}
@IBAction func showView2(_ sender: UIButton) {
view.addSubview(view2)
view2.center = CGPoint(x: view.center.x, y: 100)
}
// HIDE THE VIEWS
@IBAction func hideView1(_ sender: UIButton) {
view1.removeFromSuperview()
}
@IBAction func hideView2(_ sender: UIButton) {
view2.removeFromSuperview()
}
}
希望这有帮助!