SceneCoordinator
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
要求
- iOS 10.0 及更高版本
- Xcode 9.0 及更高版本
- Swift 4.0
安装
SceneCoordinator 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到 Podfile 中
pod 'SceneCoordinator'
用法
描述
SceneCoordinator的作用是为了简化iOS中UIViewControllers之间的导航,使得
- UIViewControllers之间能够相互独立
- 不再需要UIViewControllerDelegates。
- 导航代码尽可能简短,最多到一行。
示例
// Push Operation
SceneCoordinator<Main>.push(to: .firstViewController, animated: true)
// Presen Operation
SceneCoordinator<PresentNav>.presentNav(with: .navFirstChildViewController, animated: true)
// Pop Operation
SceneCoordinator<Main>.popToPrevious(animated: true)
// Dimiss operation
SceneCoordinator<Nav>.dismiss(animated: true)
// TabSelection Operation
SceneCoordinator<Tab>.select(1)
设置
设置,理想情况下是一个枚举,并与SceneType
协议一致
enum Main{
case first
}
extension Main : SceneType{
var storyboard: String {
return "Main"
}
var viewControllerType: UIViewController.Type {
return FirstViewController.self
}
var storyboardBundle: Bundle? {
return nil
}
}
注意1:您无需自己创建Nav
和Tab
场景类型。它们默认由SceneCoordinator
框架定义,用于执行
dismiss
函数的Nav
selectTab
函数的Tab
注意2:框架通过Storyboard中的ID获取UIViewControllers,因此请将每个UIViewController的StoryboardID命名为与类名相同的名称
现在,您可以导航到在Main
中定义的任何viewControllers。例如
SceneCoordinator<Main>.push(to: .first, animated: true)
传递数据
push
,present
和Tab.select
都提供了接受格式为[String : Any]
的数据参数的重载函数。
// push
SceneCoordinator<Main>.push(to: .firstViewController, withData: ["data" : "FromMain"], animated: true)
// present
SceneCoordinator<PresentSingleView>.presentView(scene: .presentSingleFirstViewController, withData: ["data" : text], animated: true)
// Tab.select
SceneCoordinator<Tab>.select(2, withData: ["push" : true])
获取数据
在ViewController中重写willMoveToInterface
以从push
,present
和Tab.select
操作获取数据。
override func willMoveToInterface(with data: [String : Any]) {
// `From:` is a framework defined key
// Read the value to know which viewController initiated the navigation
if let fromViewController = data["From:"]{
}
if let data = data["data"] as? String{
labelContent = data
}
}
重写willRevealOnInterface
以从pop
和Nav.dismiss
操作获取数据。
override func willRevealOnInterface(with data: [String : Any]) {
if let data = data["data"] as? String{
label.text = data
}
}
全部操作列表
push
pop
当前
presentNav
(视图控制器将被嵌入指定的UINavigationController
类型中)dismiss
(仅通过SceneCoordinator
可用)select
(仅通过SceneCoordinator<黑木耳>
可用)
作者
林海瑞。邮箱:[email.breaktyl
许可证
SceneCoordinator 根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。