Luk*_*e97 22 protocols mvvm uitabbarcontroller ios swift
我正在玩MVVM-C架构,但我不确定如何在选择选项卡时使用不同的选项卡实例化多个协调器.
这是我的主要应用程序协调员类......
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?
Bru*_*cha 29
我的协调员结构与您的不同,但它可能会对您有所帮助.就我而言,协调器协议有一个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 }    
}
对于这个问题我想分享一个例子。我的方法有点不同,并TabCoordinator没有包含他体内的所有内容。相反,每个协调器都与其控制器有关系,并且每个UIViewController(在选项卡栏控制器中)都有其UITabBarController参考,如委托模式。
class Coordinator {
    var navigationController: UINavigationController?
    var childCoordinators: [Coordinator] = [Coordinator]()
    init(with navigation: UINavigationController) {
        self.navigationController = navigation
    }
    func start() {}
}
然后是协调员。
家庭协调员
final class HomeCoordinator: Coordinator {
    var currentController: HomeController?
    weak var tabController: TabController?
    override init(with navigation: UINavigationController) {
        super.init(with: navigation)
        currentController = HomeController()
        currentController?.coordinator = self
        childCoordinators.append(self)
    }
    override func start() {
        navigationController?.pushViewController(currentController ?? UIViewController(),
                                                 animated: true)
    }
    public func getHomeData() {
        // GETTING HOME DATA
        tabController?.requestFromHomeController()
    }
}
关于协调员
final class AboutCoordinator: Coordinator {
    var currentController: AboutController?
    weak var tabController: TabController?
    override init(with navigation: UINavigationController) {
        super.init(with: navigation)
        currentController = AboutController()
        currentController?.coordinator = self
        childCoordinators.append(self)
    }
    override func start() {
        navigationController?.pushViewController(currentController ?? UIViewController(),
                                                 animated: true)
    }
    public func getAboutData() {
        // GETTING ABOUT DATA
        tabController?.requestFromAboutController()
    }
}
UITabBar控制器
final class TabController: UITabBarController {
    weak var coordinator: MainTabCoordinator?
    override func viewDidLoad() {
        super.viewDidLoad()
        let navigationController1 = UINavigationController()
        let coordinator1 = HomeCoordinator(with: navigationController1)
        coordinator1.tabController = self
        coordinator1.currentController?.tabBarItem = UITabBarItem(title: "HOME",
                                                                  image: nil,
                                                                  tag: 11)
        let navigationController2 = UINavigationController()
        let coordinator2 = AboutCoordinator(with: navigationController2)
        coordinator2.tabController = self
        coordinator2.currentController?.tabBarItem = UITabBarItem(title: "ABOUT",
                                                                  image: nil,
                                                                  tag: 22)
        viewControllers = [
            coordinator1.currentController!,
            coordinator2.currentController!
        ]
        tabBar.barTintColor = UIColor.white
        tabBar.isTranslucent = false
    }
    public func requestFromHomeController() {
        print("Home Triggered the function")
        coordinator?.fetchHome(with: "Simple Data",
                               completion: { (dictionary, error) in
                                print("dict from home -> ", dictionary)
        })
    }
    public func requestFromAboutController() {
        print("About Triggered the function")
        coordinator?.handleAbout(with: "Simple Data",
                                 completion: { (dictionary, error) in
                                    print("dict from about -> ", dictionary)
        })
    }    
}
从 AppDelegate 准备应用程序
在应用程序(_应用程序:UIApplication,didFinishLaunchingWithOptions函数
let appNavigationController = UINavigationController()
let tabCoordinator = MainTabCoordinator(with: appNavigationController ?? UINavigationController())
tabCoordinator.start()
window?.rootViewController = appNavigationController
这是关于控制器
final class AboutController: UIViewController{
    weak var coordinator: AboutCoordinator?
    // define a button and add its target to handleButton function
    @objc private func handleButton(_ sender: UIButton) {
        coordinator?.getAboutData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // ui settings
    }
}
主选项卡协调器
final class MainTabCoordinator: Coordinator {
    var currentController: TabController?
    override init(with navigation: UINavigationController) {
        super.init(with: navigation)
        self.currentController = TabController()
        self.childCoordinators.append(self)
        currentController?.coordinator = self
    }
    override func start() {
        navigationController?.pushViewController(currentController ?? UIViewController(),
                                                 animated: true)
    }
    func fetchHome(with title: String, completion:  @escaping (_ result: Dictionary<String,Any>, _ error: NSError?) -> ()) {
        completion(["ss":"ss"], nil)
    }
    func handleAbout(with title: String, completion: @escaping (_ result: Dictionary<String,Any>, _ error: NSError?) -> ()) {
        completion(["ss":"ss"], nil)
    }
}
工作架构如下:
AboutController---> 触发的操作 ---> AboutCoordinator--->TabBarController reference有关于操作的请求 ---->MainTabCoordinator处理工作。
| 归档时间: | 
 | 
| 查看次数: | 5076 次 | 
| 最近记录: |