uitabbarcontroller - 将容器控制器(根视图控制器)设置为 UITabBarController 中视图控制器的委托
问题描述
我正在尝试将容器控制器(根视图控制器)设置为选项卡栏控制器中视图控制器的导航栏项的委托,并且无法弄清楚如何在不使用的情况下执行此操作:
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
这段代码可以按我的意愿工作:
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let doghouseViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DoghouseVC") as? DoghouseViewController else { return }
doghouseViewController.delegate = rootViewController as? DoghouseViewControllerDelegate
let nav1 = UINavigationController(rootViewController: doghouseViewController)
guard let statsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "StatsVC") as? StatsViewController else { return }
statsViewController.delegate = rootViewController as? StatsViewControllerDelegate
let nav2 = UINavigationController(rootViewController: statsViewController)
dailyWrapUpViewController = DailyWrapUpViewController()
guard let calendarViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CalendarVC") as? CalendarViewController else { return }
calendarViewController.delegate = rootViewController as? CalendarViewControllerDelegate
let nav3 = UINavigationController(rootViewController: calendarViewController)
guard let settingsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsVC") as? SettingsViewController else { return }
settingsViewController.delegate = rootViewController as? SettingsViewControllerDelegate
let nav4 = UINavigationController(rootViewController: settingsViewController)
doghouseViewController.tabBarItem = UITabBarItem(title: "Doghouse", image: UIImage(named: "doghouse"), tag: 0)
statsViewController.tabBarItem = UITabBarItem(title: "Stats", image: UIImage(named: "stats"), tag: 1)
// Custom Doggy Bag Button
let controller2 = UIViewController()
calendarViewController.tabBarItem = UITabBarItem(title: "Calendar", image: UIImage(named: "calendar"), tag: 3)
settingsViewController.tabBarItem = UITabBarItem(title: "More", image: UIImage(named: "more"), tag: 4)
viewControllers = [nav1, nav2, controller2, nav3, nav4]
// Use the view controller reference to select the second tab
selectedViewController = nav1
但是,“keyWindow”在 iOS 13.0 中已被弃用,如果我尝试使用以下示例更改它,则委托功能不起作用(按钮被按下,但容器控制器实例不接收):
let rootViewController = UIApplication.shared.windows.first!.rootViewController as! ContainerController
更多上下文,容器控制器(根视图控制器)是一个侧边菜单,点击 TabBarController 内的 NavBar Item 时会滑出。
我该如何解决这个问题?
解决方案
弄清楚了:
let sceneDelegate = UIApplication.shared.connectedScenes.first!.delegate as! SceneDelegate
guard let doghouseViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DoghouseVC") as? DoghouseViewController else { return }
doghouseViewController.delegate = sceneDelegate.window!.rootViewController as? DoghouseViewControllerDelegate
let nav1 = UINavigationController(rootViewController: doghouseViewController)
guard let statsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "StatsVC") as? StatsViewController else { return }
statsViewController.delegate = sceneDelegate.window!.rootViewController as? StatsViewControllerDelegate
let nav2 = UINavigationController(rootViewController: statsViewController)
dailyWrapUpViewController = DailyWrapUpViewController()
guard let calendarViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CalendarVC") as? CalendarViewController else { return }
calendarViewController.delegate = sceneDelegate.window!.rootViewController as? CalendarViewControllerDelegate
let nav3 = UINavigationController(rootViewController: calendarViewController)
guard let settingsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsVC") as? SettingsViewController else { return }
settingsViewController.delegate = sceneDelegate.window!.rootViewController as? SettingsViewControllerDelegate
let nav4 = UINavigationController(rootViewController: settingsViewController)
doghouseViewController.tabBarItem = UITabBarItem(title: "Doghouse", image: UIImage(named: "doghouse"), tag: 0)
statsViewController.tabBarItem = UITabBarItem(title: "Stats", image: UIImage(named: "stats"), tag: 1)
// Custom Doggy Bag Button
let controller2 = UIViewController()
calendarViewController.tabBarItem = UITabBarItem(title: "Calendar", image: UIImage(named: "calendar"), tag: 3)
settingsViewController.tabBarItem = UITabBarItem(title: "More", image: UIImage(named: "more"), tag: 4)
viewControllers = [nav1, nav2, controller2, nav3, nav4]
// Use the view controller reference to select the second tab
selectedViewController = nav1
推荐阅读
- mysql - 如何根据其他表行值选择数据?
- docker - Docker 守护程序和客户端/CLI 的场景在不同的盒子上?
- javascript - 按钮单击获取所有值而不是索引
- fortran - 并行和串行的数组表达式与显式循环
- python - AttributeError:加载 tf.compat.v1.train.SessionRunHook 时模块“tensorflow”没有属性“compat”
- vue.js - Vuetify 日历。如何在“日”视图中放入一行,该视图当前在一列中包含小时数?
- python - python:如何按所有键对列表进行排序?
- javascript - 当兄弟重新渲染时,如何使反应组件不重新渲染?
- javascript - 正确创建用户后 Firebase Firestore 自定义 ID 未创建新文档
- scikit-learn - 如何知道哪些特征在预测模型中贡献显着?