2017-09-29 18 views
10

मैं एमवीवीएम-सी आर्किटेक्चर के साथ खेल रहा हूं, लेकिन मुझे यकीन नहीं है कि जब टैब का चयन किया जाता है तो मैं अलग-अलग टैब के साथ एकाधिक समन्वयक कैसे कर सकता हूं।मैं 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 संपत्ति है जो समन्वयक के व्यू कंट्रोलर को इंगित करती है।

मेरे 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 जोड़ता है।

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 के मूल संस्करण:

यहाँ NavigationCoordinator की एक बुनियादी कार्यान्वयन है

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

मैं अपने NavigationCoordinators में से एक का एक उदाहरण देख सकते हैं? देर से उत्तर के लिए क्षमा करें। –

+1

मैंने एक उदाहरण शामिल करने के लिए अपना जवाब संपादित किया :) –

संबंधित मुद्दे