iOS Swift Code - просто работает, когда у меня есть точка останова на нем - PullRequest
0 голосов
/ 23 февраля 2019

У меня есть координатор вкладок, который выходит из родительского координатора;я хочу назначить делегата для 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
  }
}


1 Ответ

0 голосов
/ 23 февраля 2019

OK.Я решил это;Первой мыслью было, что это проблема нитей или расы;но казалось, что это так, поскольку делегат является слабой ссылкой;это не было сохранено, НО, когда у меня есть точка останова, IDE сохранил делегата, таким образом это работало.решение состояло в том, чтобы иметь родительский делегат в качестве хранимого свойства, чтобы родительский элемент оставался у родителей.

...