[英]Access the ROOT detailViewController from the masterVewController in a splitviewcontroller
Here is the layout of my app这是我的应用程序的布局
I am trying to access a variable of startViewController from within TableViewController.我正在尝试从 TableViewController 中访问 startViewController 的变量。
When I print the viewControllers of splitViewController ( print(self.splitViewController?.viewControllers)
) from within tableViewController this is what I get当我从 tableViewController print(self.splitViewController?.viewControllers)
splitViewController ( print(self.splitViewController?.viewControllers)
) 的print(self.splitViewController?.viewControllers)
,这就是我得到的
Optional([<UINavigationController: 0x12200f600>, <temp.CollectionViewController: 0x121e16860>])
That second viewController( temp.CollectionViewController
) should be of type startViewController.第二个 viewController( temp.CollectionViewController
) 应该是 startViewController 类型。
The startViewController in my program presents the collectionViewController using a segue with kind show.我的程序中的 startViewController 使用带有 kind show 的 segue 来呈现 collectionViewController。 The collectionViewController has already been segued to when I call print(self.splitViewController?.viewControllers)
.当我调用print(self.splitViewController?.viewControllers)
时, collectionViewController 已经被转移到了。
try to cast it as below :尝试将其转换如下:
splitViewController?.viewControllers.forEach({ (vc) in
if let startController = vc as? startViewController {
--- Your code ---
return
}
})
I ended up needing to put StartViewController inside a UINavigationController.我最终需要将 StartViewController 放在 UINavigationController 中。 I was then able to access the first viewController with然后我就可以访问第一个 viewController
var detailStart = (splitViewController?.viewControllers[1] as? UINavigationController)?.viewControllers.first as? StartViewController
I then hid the unwanted navigation bar with然后我隐藏了不需要的导航栏
navigationController?.setNavigationBarHidden(true, animated: false)
Which was placed inside viewDidAppear放在 viewDidAppear 里面
This ended up being an ok solution for me, because I needed to put CollectionViewController inside a UINavigationController anyway, but it seems kind of hacky and maybe not the best practice这最终对我来说是一个不错的解决方案,因为无论如何我都需要将 CollectionViewController 放在 UINavigationController 中,但这似乎有点hacky,可能不是最佳实践
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.