SceneCoordinator 0.1.2

SceneCoordinator 0.1.2

lin hairui 维护。



  • 作者:
  • linhairui19@

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:您无需自己创建NavTab场景类型。它们默认由SceneCoordinator框架定义,用于执行

  • dismiss函数的Nav
  • selectTab函数的Tab

注意2:框架通过Storyboard中的ID获取UIViewControllers,因此请将每个UIViewController的StoryboardID命名为与类名相同的名称

screen shot 2018-09-21 at 4 21 20 pm

现在,您可以导航到在Main中定义的任何viewControllers。例如

SceneCoordinator<Main>.push(to: .first, animated: true)

传递数据

pushpresentTab.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以从pushpresentTab.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以从popNav.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 文件。