2017-09-29 39 views
10

MVVM-C mimarisi ile oynuyorum, ancak bir sekme seçildiğinde farklı sekmelerle birden çok koordinatörü nasıl oluşturabileceğime emin değilim. Koordinatörleri bir UIITabBarController ile nasıl kullanırım?

İşte benim ana uygulama koordinatörü sınıfı var

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

Yani sekmesi seçildiğinde i PlansCoordinator akım koordinatör (ProfileCoordinator) gitmek ne kadar?

cevap

4

Koordinatör yapım sizinkilerden farklı, ancak size yardımcı olabilir. Benim durumumda, Koordinatör protokolü, Koordinatörün ViewController'ına işaret eden rootViewController özelliğine sahiptir. , Kalıcı koordinatörler NavigationControllers tutun özel Koordinatörleri olan, NavigationCoordinators olan gerçek kodunda (Bu örnekte sadece ViewControllers ve kaldırılır bellek yönetimi şeyler ekledi:

AppCoordinator sonra böyle biraz görünen bir TabCoordinator, devam My. daha kolay anlaşılması için.)

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 

} 
} 

Yani temelde, sizin TabBarController kimin en rootViewController bir TabBarController bir TabCoordinator olduğunu. TabCoordinator ilgili Koordinatörleri başlatır ve ilgili rootViewControllers'u sekmeye ekler.

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

Ve Coordinator temel bir versiyonu:

İşte NavigationCoordinator temel uygulama var

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

i NavigationCoordinators birinin bir örneğini görebilir miyim? Geç cevap verdiğim için özür dilerim. –

+1

Bir yanıt eklemek için cevabımı düzenledim :) –

İlgili konular