Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift Code - Just working when i have a breakpoint on it

I have a tab coordinator that extends from a parent coordinator; i want to assign a delegate to the UITabViewController; but when i do it, it doesn't trigger anything; but if i have a breakpoint in the init function of my coordinator; it will work as expected. My brain is going to explode because i don't know where to search the bug, as it is working as expected when XCODE does have a breakpoint.

  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
  }
}


like image 699
dfmarulanda Avatar asked Nov 27 '25 03:11

dfmarulanda


1 Answers

OK. I solved it; first thought was that it was a problem about threads or a race-condition; but it seemed it was that as the delegate is a weak reference; it wasnt retained BUT when i have a breakpoint, the IDE retained the delegate, so it worked. solution was to have the delegate parent as a stored property, to have the delegate retained by the parent.

like image 93
dfmarulanda Avatar answered Nov 29 '25 20:11

dfmarulanda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!