【问题标题】:iOS Swift Code - Just working when i have a breakpoint on itiOS Swift 代码 - 当我有断点时才工作
【发布时间】:2019-07-17 01:27:10
【问题描述】:

我有一个从父协调器扩展而来的选项卡协调器;我想为 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
  }
}


【问题讨论】:

  • 如何设置断点? (是哪一行,被禁用了吗?)
  • 你可以在你放置断点的那一行添加abort()代码:如果程序没有崩溃,那么就没有到达那个地方。
  • @AnthonyKong 在 init 函数中的任何位置。禁用时,代码无法按预期工作;就在启用时。我使用 XCODE 设置断点
  • @OlhaPavliuk 已到达该位置,但无法按预期工作(未触发委托),它仅在我在 init 函数中的任何位置都有断点时工作,
  • self.viewDelegate 是否在父构造函数中设置? self.delegate 将被留下 nil 否则。

标签: ios swift rx-swift coordinator-pattern


【解决方案1】:

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

【讨论】:

  • 我不相信 Xcode 在触发断点时会保留属性。这可能是一个 IDE 错误或更深层次的问题。另外,请注意,将viewDelegate 设置为privatelet,您将失去它所能提供的几乎所有好处。
猜你喜欢
  • 2010-09-29
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 2018-01-25
  • 1970-01-01
相关资源
最近更新 更多