2017-09-29 32 views
10

我正在玩MVVM-C体系结构,但我不确定如何在选择选项卡时实例化具有不同选项卡的多个协调器。如何使用UIITabBarController协调器?

这是我主要的应用程序协调类...

protocol UINavigationControllerType: class { 
func pushViewController(_ viewController: UIViewController, animated: Bool) 
func popViewController(animated: Bool) -> UIViewController? 
} 

protocol Coordinator: class { 
func start() 
} 

final class AppCoordinator: Coordinator { 
// MARK: - Properties 
var managedObjectContext: NSManagedObjectContext! 
var coordinators = [String : Coordinator]() 

var tabController: UITabBarController? 

// MARK: - Object Lifecycle 
init(moc: NSManagedObjectContext, tabController: UITabBarController) { 
    self.managedObjectContext = moc 
    self.tabController = tabController 
} 

// MARK: - Coordinator 
func start() { 
    guard let tabController = tabController else {return} 

    let profileNavigationController = NavigationController() 
    profileNavigationController.tabBarItem = UITabBarItem(title: "Profile", image: UIImage(named: "profileUnselected"), selectedImage: UIImage(named: "profileSelected")) 

    let plansNavigationController = NavigationController() 
    plansNavigationController.tabBarItem = UITabBarItem(title: "Plans", image: UIImage(named: "plansUnselected"), selectedImage: UIImage(named: "plansSelected")) 

    tabController.viewControllers = [profileNavigationController, plansNavigationController] 
    tabController.selectedViewController = profileNavigationController 

    let profileCoordinator = ProfileCoordinator(navigationController: profileNavigationController) 
    profileCoordinator.managedObjectContext = managedObjectContext 
    coordinators["profileCoordinator"] = profileCoordinator 
    profileCoordinator.delegate = self 
    profileCoordinator.start() 
} 
} 

// MARK: - ProfileCoordinatorDelegate 
extension AppCoordinator: ProfileCoordinatorDelegate {} 

那么,如何将从目前的协调员(ProfileCoordinator)到PlansCoordinator去选择标签时?

回答

4

我的协调员结构与您的不同,但它可能对您有所帮助。在我的情况下,协调员协议有一个rootViewController属性指向该协调员的ViewController。

AppCoordinator再持续一个TabCoordinator,这看起来有点像这样:(在实际的代码中,坚持协调是NavigationCoordinators,这是保存NavigationControllers特别协调员在这个例子中,我刚刚添加的ViewControllers和取出内存管理的东西。使其更容易理解。)

final class TabCoordinator: NSObject, Coordinator { 

var rootViewController: UIViewController { 
    return tabController 
} 

let tabController: UITabBarController 

let homeCoordinator: HomeCoordinator 
let historyCoordinator: HistoryCoordinator 
let profileCoordinator: ProfileCoordinator 

var coordinators: [Coordinator] { 
    return [homeCoordinator, historyCoordinator, profileCoordinator] 
} 

init(client: HTTPClient, persistence: Persistence) { 

    tabController = UITabBarController() 

    homeCoordinator = HomeCoordinator(client: client, persistence: persistence) 

    historyCoordinator = HistoryCoordinator(client: client, persistence: persistence) 

    profileCoordinator = ProfileCoordinator(client: client, persistence: persistence) 

    var controllers: [UIViewController] = [] 

    let homeViewController = homeCoordinator.rootViewController 
    homeViewController.tabBarItem = UITabBarItem(title: Localization.homeTab.string, image: Asset.iconMenuRecharge.image, selectedImage: Asset.iconMenuRechargeActivated.image) 

    let historyViewController = historyCoordinator.rootViewController 
    historyViewController.tabBarItem = UITabBarItem(title: Localization.walletTab.string, image: Asset.iconMenuWallet.image, selectedImage: Asset.iconMenuWalletActivated.image) 

    let profileViewController = profileCoordinator.rootViewController 
    profileViewController.tabBarItem = UITabBarItem(title: Localization.profileTab.string, image: Asset.iconMenuProfile.image, selectedImage: Asset.iconMenuProfileActivated.image) 

    super.init() 

    controllers.append(homeViewController) 
    controllers.append(historyViewController) 
    controllers.append(profileViewController) 

    tabController.viewControllers = controllers 
    tabController.tabBar.isTranslucent = false 
    tabController.delegate = self 

} 
} 

所以基本上,你的TabBarController是TabCoordinator它的rootViewController是一个TabBarController。 TabCoordinator实例化相关协调器并将其各自的rootViewControllers添加到选项卡。

这是一个基本实现NavigationCoordinator的:

class NavigationCoordinator: NSObject, Coordinator {  

    public var navigationController: UINavigationController  

    public override init() { 
     self.navigationController = UINavigationController() 
     self.navigationController.view.backgroundColor = .white 
     super.init() 
    }  

    public var rootViewController: UIViewController { 
     return navigationController 
    } 
} 

而一个Coordinator的基本版本:

public protocol Coordinator: class {  
    var rootViewController: UIViewController { get }  
} 
+0

我可以看到你的NavigationCoordinators的一个例子吗?这么晚才回复很抱歉。 –

+1

我编辑了我的回复以包含示例:) –