XaN*_*Ny0 5 parentviewcontroller ios uicontainerview swift
我有4个带有Headerpart的视图,我将其外包到一个containerview中,在所有4个视图上都有相同的字段和布局.在我的容器里面,我有很多标签,我知道想要填充数据.我现在的问题是,我必须根据用户选择的游戏填写标签.游戏是我的玩家类中的枚举.我不知道如何从容器视图中获取该信息并相应地设置游戏变量以执行我的代码.有没有一个解决方案从我的容器视图中的容器视图中获取storyboardid?
切换游戏
case.Coinflip:
Player1PointsLabel.Text =(player1.points.coinflip)
case.RollingDices
Player1PointsLabel.Text =(player1.points.rollingdices)
也许我做错了什么,设计明智,我不是那么有经验,所以我也愿意接受建议.
最好的祝福
Eri*_*ard 12
据我所知,获取插入到ContainerView中的视图的ViewController的唯一方法是在实例化ContainerView时在父ViewController中保存对它的引用.
Swift 4示例:
如果您在故事板中使用了ContainerView并添加了嵌入segue:
var containerVC: UIViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourEmbedSegueName" {
if let vc = segue.destination as? YourViewController {
self.containerVC = vc
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您以编程方式在ContainerView中插入View:
var containerVC: UIViewController?
func customContainerEmbed(vc: UIViewController) {
self.addChildViewController(vc)
yourContainer.addSubview(vc.view)
vc.view.frame = yourContainer.bounds
vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
vc.didMove(toParentViewController: self)
self.containerVC = vc
}
Run Code Online (Sandbox Code Playgroud)
你的问题的目的不是很明确。
如果您想访问视图的超级视图(包含子视图的视图),请使用“myView.superview”。
如果您想访问托管 UIViewController 的 UIViewController,请使用“myViewController.presentingViewController”。
最后,如果要访问托管视图的 UIViewController,则必须遍历响应者链,直到到达第一个 UIViewController 或链的末尾(UIView 是 UIResponder 的子类):
func viewController(forView: UIView) -> UIViewController? {
var nr = forView.next
while nr != nil && !(nr! is UIViewController) {
nr = nr!.next
}
return nr as? UIViewController
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3156 次 |
| 最近记录: |