3

我有一个从父协调器扩展而来的选项卡协调器;我想为 UITabViewController 分配一个委托;但是当我这样做时,它不会触发任何事情;但是如果我的协调器的 init 函数中有一个断点;它将按预期工作。我的大脑要爆炸了,因为我不知道在哪里搜索错误,因为当 XCODE 确实有断点时它按预期工作。

  import RxSwift

enum HomeRoutes: Route{
  case explore
  case swaps
  case post
  case notifications
  case profile
}


class HomeCoordinator: ViewCoordinator<HomeRoutes> {

  typealias Dependencies =  HasUserManager & HasItemService

  // MARK: - Stored properties
  private let viewDelegate = CrowdswapTabDelegate()
  private let disposeBag = DisposeBag()

  convenience init(dependencies: Dependencies) {

    //Create Tabs
    let exploreTab = ExploreCoordinator(dependencies: dependencies)
    exploreTab.rootViewController.navigationBar.isHidden = true
    exploreTab.rootViewController.tabBarItem = UITabBarItem(title: nil, image: #imageLiteral(resourceName: "explore"), selectedImage: nil)
    exploreTab.rootViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

    let swapsTab = SwapsCoordinator(dependencies:dependencies)
    swapsTab.rootViewController.navigationBar.isHidden = true
    swapsTab.rootViewController.tabBarItem = UITabBarItem(title: nil, image: #imageLiteral(resourceName: "swaps"), selectedImage: nil)
    swapsTab.rootViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

    let postTab = PostCoordinator(dependencies: dependencies)
    postTab.rootViewController.navigationBar.isHidden = true
    postTab.rootViewController.tabBarItem = UITabBarItem(title: nil, image: #imageLiteral(resourceName: "upload"), selectedImage: nil)
    postTab.rootViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)


    let notificationTab = NotificationCoordinator()
    notificationTab.rootViewController.navigationBar.isHidden = true
    notificationTab.rootViewController.tabBarItem = UITabBarItem(title: nil, image: #imageLiteral(resourceName: "notifications"), selectedImage: nil)
    notificationTab.rootViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

    let profileTab = ProfileCoordinator(dependencies: dependencies)
    profileTab.rootViewController.navigationBar.isHidden = true
    profileTab.rootViewController.tabBarItem = UITabBarItem(title: nil, image: #imageLiteral(resourceName: "profile"), selectedImage: nil)
    profileTab.rootViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

    let tabBarController = UITabBarController()
    tabBarController.tabBar.tintColor = UIColor(red:0.21, green:0.17, blue:0.46, alpha:1.0)
    tabBarController.tabBar.backgroundColor = UIColor.white
    tabBarController.tabBar.isTranslucent = false
    tabBarController.tabBar.backgroundImage = UIImage()
    tabBarController.tabBar.layer.borderWidth = 0.0
    tabBarController.tabBar.clipsToBounds = true

    tabBarController.viewControllers = [exploreTab.rootViewController, swapsTab.rootViewController, postTab.rootViewController, notificationTab.rootViewController, profileTab.rootViewController]

    self.init(controller: tabBarController)

  }

  // MARK: - Init
  init(controller: UITabBarController) {
    controller.delegate = viewDelegate
    super.init(root: controller)
  }
}


And here is my viewDelegate:


class CrowdswapTabDelegate: NSObject, UITabBarControllerDelegate {

  func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    /// Prevent selection of the same tab twice (which would reset its navigation controller)
    if viewController.tabBarItem.image == #imageLiteral(resourceName: "upload") {
      return false
    }
    if let viewController = viewController.children[0] as? ExploreViewController{
      if tabBarController.selectedIndex == 0 {
        viewController.collectionView.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
      }
      return true
    }
    return true
  }
}


4

1 回答 1

1

好的。我解决了;第一个想法是这是线程或竞争条件的问题;但似乎代表是一个弱参考;它没有保留但是当我有一个断点时,IDE 保留了委托,所以它起作用了。解决方案是将委托父级作为存储属性,由父级保留委托。

于 2019-02-23T03:34:49.753 回答