简介
HYRouter 是我在进行模块开发的应用时构建的一个路由框架。它帮助您解耦 ViewControllers,在调试的 UITableViewController 中运行所有的 ViewControllers,并灵活地设置主要的 UITabbarController。
如果您使用苹果提供的传统导航方法,您不能解耦两个模块。每次导航到 B_ViewController 时,您都必须初始化 B_ViewController 的一个对象。这意味着您必须知道 B_ViewController 类的存在。当 B_ViewController 来自不同的模块时,解耦模块就变得非常困难。
功能
- 通过 ViewController 标识符进行导航
- 轻松在 ViewControllers 之间传递数据
- 支持带有或没有 storyboard 的 ViewController
- 通过字典配置主 UITabBarController
- 在调试模式下为所有 ViewController 设置条目
安装
您可以通过将 HYRouter 添加到 Podfile
中来使用 CocoaPods 安装 HYRouter
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'HYRouter'
然后运行 pod install
。
用法
路由器
1 - 实现RouterController协议
import HYRouter
class MyViewController:UIViewController, RouterController {
var params: [String : Any]?
}
前一个ViewController的所有数据都将在这里
2 - 准备传递所需的数据
let param = RouterParams()
param["videoUrl"] = "https://video.com/id=123"
3 - 调用Navigate方法
navigate(controllerName: String, params:RouterParams,isPresent: Bool)
- controllerName:当下一个ViewController使用Storyboard时,controllerName是Storyboard标识符。如果不使用Storyboard,controllerName是ViewController类名
- params:数据以字典结构配置
- isPresent:对于presentarViewController为真,对于pushViewController为假
4 - 接收数据
import HYRouter
class MyViewController:UIViewController, RouterController {
var params: [String : Any]? {
didSet {
guard let params = params else { return }
if params.keys.contains("videoUrl")
{ self.urlString = params["videoUrl"] as? String }
}
}
}
DebugViewController
AppDelegate.swift
import HYRouter
let vcs = ["Record","ActityHisController","History"]
window?.rootViewController = DebugLaucher.tableController(controllers: vcs)
主界面选项卡控制器
AppDelegate.swift
import HYRouter
let vcs = ["Record","ActityHisController","History"]
window?.rootViewController = DebugLaucher.tabController(controllers: vcs)