将容器控制器(根视图控制器)设置为 UITabBarController 中视图控制器的委托

Posted

技术标签:

【中文标题】将容器控制器(根视图控制器)设置为 UITabBarController 中视图控制器的委托【英文标题】:Setting Container Controller (Root View Controller) as Delegate for View Controllers in UITabBarController 【发布时间】:2020-04-18 18:17:30 【问题描述】:

我正在尝试将容器控制器(根视图控制器)设置为选项卡栏控制器中视图控制器的导航栏项的委托,并且无法弄清楚如何在不使用的情况下执行此操作:

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 时会滑出。

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

想通了:

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

【讨论】:

以上是关于将容器控制器(根视图控制器)设置为 UITabBarController 中视图控制器的委托的主要内容,如果未能解决你的问题,请参考以下文章

设置视图控制器,例如 Find My Friends iPad 应用

如何将根视图控制器默认为 1024x768 大小的横向

为视图控制器根视图设置外观代理

如何设置新的根视图控制器

动态设置拆分视图控制器为根视图控制器IOS

以编程方式设置为根视图控制器时,视图控制器无法正确显示子视图