我有一个标签协调器,它从父协调器延伸而来;我想为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
}
}
答案 0 :(得分:1)
好。我解决了;首先想到的是关于线程或竞争条件的问题;但似乎由于该代表是一个薄弱的参考;当我有一个断点时,它并没有保留,但IDE保留了委托,因此它可以工作。 解决方案是将委托的父级作为存储属性,使委托由父级保留。