ios - 访问嵌入在 UIViewController 中的 UITabBarController
问题描述
我在 UIViewController 中嵌入了一个 UITabBarController。我正在尝试从父 UIViewController 访问 UITabBarController 实例。
vc.children 的返回类型为 [UIViewController],因此 UITabBarController 不会出现。
vc.tabBarController 为 nil,因为视图未嵌入 TabBarController。我的设置是相反的。
有任何想法吗?
我的故事板:
解决方案
Fastest solution:
vc.children.compactMap({$0 as? UITabBarController}).first
.
Best solution:
Select the Embed Segue
from the storyboard and give an identifier (say "containerEmbedSegue". Next, in your vc:
var tabBarVC: UITabBarController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "containerEmbedSegue" {
self.tabBarVC = segue.destination as? UITabBarController
}
}
推荐阅读
- bash - 在不使用标准循环的情况下从 bash 中读取文件 - 非典型用例
- r - 如何在 R 中以 1 分钟为基础分布多个时间间隔值?
- java - 当用户在Android中关闭应用程序时如何重新启动应用程序
- reactjs - LocalStorage 未使用 React 钩子更新状态中的属性
- .net-core - 寻找一种方法来更改 http 请求并在正文数据上添加用户 ID
- coq - compare.vo 包含库 Top.comparable 而不是库可比较
- flutter - 如何以颤动的 TimeOfDay 以 AM 或 PM 格式显示时间?
- c++ - 为什么以及何时在 C++ 中的下界函数前面添加指针?
- powershell - 使用 powershell 获取超过 1 小时的打印作业
- unix - 如何将 OCaml 信号转换为 POSIX 信号或字符串?