|
1 | 1 | // |
2 | | -// Router.swift |
| 2 | +// UIViewControllerRouter.swift |
3 | 3 | // |
4 | 4 | // Created by wxlpp on 2021/5/1. |
5 | 5 | // Copyright © 2021 Wang Xiaolong. All rights reserved. |
6 | 6 | // |
7 | 7 |
|
8 | 8 | import UIKit |
9 | 9 |
|
10 | | -public protocol RouteBase: UIViewController { |
11 | | - static var path: String { get } |
12 | | - static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>) |
13 | | -} |
14 | | - |
15 | | -/// 需要通过路由解耦的 `UIViewController` 实现此协议后,路由中心将会自动完成组件的注册 |
16 | | -public protocol Routable: RouteBase { |
17 | | - |
18 | | - /// 路由中心通过路由链接到指定 `UIViewController` 后由此回调进行页面构造 |
19 | | - /// - Parameters: |
20 | | - /// - parameters: 由路由 `URL` 解析后传递的参数 |
21 | | - /// - completion: 验证参数后构造页面通过回调返回结果 |
22 | | - static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<Self>) |
23 | | -} |
24 | | - |
25 | | -public extension Routable { |
26 | | - static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>) { |
27 | | - route(parameters: parameters) { result in |
28 | | - completion(result.map { $0 as UIViewController }) |
29 | | - } |
30 | | - } |
31 | | -} |
32 | | - |
33 | | - |
34 | 10 | /// 一个路由器可以通过解析链接获得一个指定的`UIViewController`实例 |
35 | 11 | public class UIViewControllerRouter { |
36 | 12 | private var isInitialized = false |
@@ -91,8 +67,10 @@ public class UIViewControllerRouter { |
91 | 67 | for i in 0 ..< numberOfClasses { |
92 | 68 | let item: AnyClass = classesPtr[i] |
93 | 69 | if let vcType = item as? RouteBase.Type { |
94 | | - try? urlRouter.register(route: vcType.path) { parameters, completion in |
95 | | - vcType.routeVC(parameters: parameters, completion: completion) |
| 70 | + for path in vcType.paths { |
| 71 | + try? urlRouter.register(route: path) { parameters, completion in |
| 72 | + vcType.routeVC(parameters: parameters, completion: completion) |
| 73 | + } |
96 | 74 | } |
97 | 75 | } |
98 | 76 | } |
|
0 commit comments