我正在尝试将容器控制器(根视图控制器)设置为选项卡栏控制器中视图控制器的导航栏项的委托,并且无法弄清楚如何在不使用的情况下执行此操作:
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 时会滑出。
我该如何解决这个问题?