我正在使用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?
答案 0 :(得分:24)
我的协调员结构与您的不同,但它可能会对您有所帮助。就我而言,协调器协议有一个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 }
}
答案 1 :(得分:1)
我想分享这个问题的例子。我的方法有些不同,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()
}
}
UITabBarController
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
这里是AboutController
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
}
}
MainTabCoordinator
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
处理工作。