协调器模式

协调器模式(Coordinator Pattern)

协调器模式是一种结构型设计模式,用来组织viewcontroller之间的跳转逻辑。它包含以下部分

  1. coordinator:定义了所有concrete coordinators必须实现的方法和属性。具体来说,它定义关系属性,子级和router。它还定义了呈现方法,present和dismiss。通过持有coordinator protocol而不是直接持有concrete coordinators,你可以将父协调器与其子协调器解耦。这也使父协调员可以在单个属性中保留各种具体的子协调员,即children
  2. concrete coordinator:实现了coordinator protocol。它知道如何创建具体的view controller以及view controller的显示顺序。
  3. router:一种协议,定义了所有 concrete router必须实现的方法。具体来说,它定义了用于显示和关闭视图控制器的present和dismiss方法
  4. concrete router:知道如何显示view controller,但不知道在显示什么或接下来将显示哪个view controller。相反,coordinator告诉router要显示哪个view controller
  5. concrete view controllers:mvc中的view controller,他们不知道其他的view controller。他们是coordinator的代理。

使用举例

1.创建Router protocol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public protocol Router: class {
func present(_ viewController: UIViewController, animated: Bool)
func present(_ viewController: UIViewController,
animated: Bool,
onDismissed: (()->Void)?)
func dismiss(animated: Bool)
}
extension Router {
public func present(_ viewController: UIViewController,
animated: Bool) {
present(viewController, animated: animated, onDismissed: nil)
}
}

2.创建Concrete Router

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class NavigationRouter: NSObject {
private let navigationController: UINavigationController
private let routerRootController: UIViewController?
private var onDismissForViewController:
[UIViewController: (() -> Void)] = [:]
public init(navigationController: UINavigationController) {
self.navigationController = navigationController
self.routerRootController = navigationController.viewControllers.first
super.init()
navigationController.delegate = self
}
}
// MARK: - Router
extension NavigationRouter: Router {
public func present(_ viewController: UIViewController,
animated: Bool,
onDismissed: (() -> Void)?) {
onDismissForViewController[viewController] = onDismissed
navigationController.pushViewController(viewController,
animated: animated)
}
public func dismiss(animated: Bool) {
guard let routerRootController = routerRootController else {
navigationController.popToRootViewController(animated: animated)
return
}
performOnDismissed(for: routerRootController)
navigationController.popToViewController(routerRootController,
animated: animated)
}
private func performOnDismissed(for viewController: UIViewController) {
guard let onDismiss = onDismissForViewController[viewController]
else {
return
}
onDismiss()
onDismissForViewController[viewController] = nil
}
}
// MARK: - UINavigationControllerDelegate
extension NavigationRouter: UINavigationControllerDelegate {
public func navigationController(_ navigationController: UINavigationController,
didShow viewController: UIViewController,
animated: Bool) {
guard let dismissedViewController = navigationController.transitionCoordinator?.viewController(forKey: .from),
!navigationController.viewControllers.contains(dismissedViewController) else { return }
performOnDismissed(for: dismissedViewController)
}
}

3.创建Coordinator protocol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public protocol Coordinator: class {
var children: [Coordinator] { get set }
var router: Router { get }
func present(animated: Bool, onDismissed: (() -> Void)?)
func dismiss(animated: Bool)
func presentChild(_ child: Coordinator,
animated: Bool,
onDismissed: (() -> Void)?)
}
extension Coordinator {
public func dismiss(animated: Bool) {
router.dismiss(animated: true)
}
public func presentChild(_ child: Coordinator,
animated: Bool,
onDismissed: (() -> Void)? = nil) {
children.append(child)
child.present(animated: animated, onDismissed: { [weak self, weak child] in
guard let self = self, let child = child else { return }
self.removeChild(child)
onDismissed?()
})
}
private func removeChild(_ child: Coordinator) {
guard let index = children.firstIndex(where: { $0 === child })
else {
return
}
children.remove(at: index)
}
}

4.创建Concrete Coordinator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public class HowToCodeCoordinator: Coordinator {
// MARK: - Instance Properties
public var children: [Coordinator] = []
public let router: Router
private lazy var stepViewControllers = [
StepViewController.instantiate(
delegate: self,
buttonColor: UIColor(red: 0.96, green: 0, blue: 0.11, alpha: 1),
text: "When I wake up, well, I'm sure I'm gonna be\n\n" +
"I'm gonna be the one writin' code for you",
title: "I wake up"),
StepViewController.instantiate(
delegate: self,
buttonColor: UIColor(red: 0.93, green: 0.51, blue: 0.07, alpha: 1),
text: "When I go out, well, I'm sure I'm gonna be\n\n" +
"I'm gonna be the one thinkin' bout code for you",
title: "I go out"),
StepViewController.instantiate(
delegate: self,
buttonColor: UIColor(red: 0.23, green: 0.72, blue: 0.11, alpha: 1.0),
text: "Cause' I would code five hundred lines\n\n" +
"And I would code five hundred more",
title: "500 lines"),
StepViewController.instantiate(
delegate: self,
buttonColor: UIColor(red: 0.18, green: 0.29, blue: 0.80, alpha: 1.0),
text: "To be the one that wrote a thousand lines\n\n" +
"To get this code shipped out the door!",
title: "Ship it!")
]
private lazy var startOverViewController =
StartOverViewController.instantiate(delegate: self)
// MARK: - Object Lifecycle
public init(router: Router) {
self.router = router
}
// MARK: - Coordinator
public func present(animated: Bool, onDismissed: (() -> Void)?) {
let viewController = stepViewControllers.first!
router.present(viewController, animated: animated, onDismissed: onDismissed)
}
}
// MARK: - StepViewControllerDelegate
extension HowToCodeCoordinator: StepViewControllerDelegate {
public func stepViewControllerDidPressNext(_ controller: StepViewController) {
if let viewController = stepViewController(after: controller) {
router.present(viewController, animated: true)
} else {
router.present(startOverViewController, animated: true)
}
}
private func stepViewController(after controller: StepViewController) -> StepViewController? {
guard let index = stepViewControllers.firstIndex(where: { $0 === controller }),
index < stepViewControllers.count - 1 else { return nil }
return stepViewControllers[index + 1]
}
}
// MARK: - StartOverViewControllerDelegate
extension HowToCodeCoordinator: StartOverViewControllerDelegate {
public func startOverViewControllerDidPressStartOver(_ controller: StartOverViewController) {
router.dismiss(animated: true)
}
}

5.使用协调者模式

1
2
3
4
5
6
7
8
9
10
11
let homeViewController = HomeViewController.instantiate()
let navigationController = UINavigationController(rootViewController: homeViewController)
let router = NavigationRouter(navigationController: navigationController)
let coordinator = HowToCodeCoordinator(router: router)
homeViewController.onButtonPressed = { [weak coordinator] in
coordinator?.present(animated: true, onDismissed: nil)
}
PlaygroundPage.current.liveView = navigationController

其他例子

  1. 在tableview中使用:https://blog.csdn.net/weixin_33768481/article/details/87940599
  2. 学习日本汉字的app:https://blog.csdn.net/kmyhy/article/details/80770184
  3. 使用Coordinator提高VC/ViewModel复用性:https://www.jianshu.com/p/e8fc65bcf1c8
  4. 视频学习地址:https://www.jianshu.com/p/cdd96b462ffb
  5. 基于ReSwift和App Coordinator的iOS架构:https://www.jianshu.com/p/42766da9578e