简体   繁体   English

iOS-容器视图控制器Swift

[英]iOS - Container View Controller Swift

I am new to iOS development. 我是iOS开发的新手。 On one of the app screens, I want to use segmented control with 2 segments - say A & B. A & B have different UIView. 在一个应用程序屏幕上,我想使用带有2个细分的分段控件-例如A和B。A和B具有不同的UIView。 A has tableview, B has UIImageView and textLabels. A具有表视图,B具有UIImageView和textLabels。 I know that I need to use Container View Controller. 我知道我需要使用Container View Controller。 But not sure how. 但不确定如何。 If I am not mistaken there can be only one Embedded Segue to a View Controller - then how do I get 2 UIViews. 如果我没记错的话,视图控制器只能有一个嵌入式Segue-那么我如何获得2个UIView。

I don't know objective C so its difficult to translate answers from similar questions on SO to swift. 我不了解目标C,因此很难将SO上类似问题的答案迅速翻译成。 Also don't want to go with view.hidden = false & true as I read its not good from a memory standpoint. 同样也不想使用view.hidden = false&true,因为我从内存的角度看它不好。

Can someone please explain step by step what needs to be done - how to use loadChildViewController, didMovetoParentController, etc. How will the storyboard look like. 有人可以分步说明需要做什么-如何使用loadChildViewController,didMovetoParentController等。情节提要会是什么样子。 Thanks 谢谢

There can be only one embed segue to one container. 一个容器只能有一个嵌入序列。 You'd have to create 2 containers with each one having it's own segue. 您必须创建2个容器,每个容器都有自己的序列。 Or you can add child controllers from code, just create UIView in which you want your content to be add child view controller and add child controllers' view to it. 或者,您可以从代码中添加子控制器,只需创建UIView在其中将内容添加子视图控制器并向其中添加子控制器的视图。 I created sample project to show you storyboard setup as well. 我创建了示例项目来向您展示情节提要设置。

@IBOutlet weak var containerView: UIView!

...

addChildViewController(controller)

containerView.addSubview(controller.view)
controller.view.setTranslatesAutoresizingMaskIntoConstraints(false)
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: nil, metrics: nil, views: ["view" : controller.view])
constraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: nil, metrics: nil, views: ["view" : controller.view])
NSLayoutConstraint.activateConstraints(constraints)

controller.didMoveToParentViewController(self)

Instead of using a container view why not just setup both views in the IB and then set the hidden property of a view based on the segmented control value. 除了使用容器视图之外,为什么不直接在IB中设置两个视图,然后基于分段的控制值设置视图的hidden属性。

Refer to this question for a more in-depth explanation. 请参阅此问题以获取更深入的说明。 Question here 在这里提问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM