2017-09-29 34 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

我編輯了我的回覆以包含示例:) –