StoryCode 1.0-beta.5

StoryCode 1.0-beta.5

Anton Sergeev 维护。



StoryCode 1.0-beta.5

  • 作者
  • Anton Sergeev

StoryCode. 从代码生成 Storyboards

UIViewController 进行动态依赖注入的严格类型化工具。

使用方法

要创建包含所有依赖的 UIViewController 实例,需要使用 Scene 结构

let viewController = Scene {
        return UIViewController()
    } .with {
        return FirstDependency()
    } .with {
        return SecondDependency()
    } .instantiante(connect: { viewController, firstDependency, secondDependency in
        /* make any connections */
    }, view: { viewController, firstDependency, secondDependency in
        let view = UIView()
        /* setup view and make any connections with view */
        return view
    })
}

UIViewController 和所有依赖项都可以满足 Awakable 协议

protocol Awakable {
    func awake()
}

在这种情况下,awake() 方法将在 connect 关闭后立即调用。到那时,所有不与 View 相关的依赖项都将被注入。

此外,所有依赖项都可以满足 ViewObserver 协议

public protocol ViewObserver {
    func viewDidLoad()
    func viewWillAppear(_ animated: Bool)
    func viewDidAppear(_ animated: Bool)
    func viewWillDisappear(_ animated: Bool)
    func viewDidDisappear(_ animated: Bool)
    func viewWillLayoutSubviews()
    func viewDidLayoutSubviews()
}

在这种情况下,这些方法将与 UIViewController 类似的方法一起调用。所有 ViewObserver 方法默认为空实现。

安装

Carthage: github "antonsergeev88/StoryCode"

描述

框架的关键元素是场景。场景是一个包含辅助对象的 UIViewController。所有对象的生命周期同步,UIViewController 保留对所有辅助对象的强引用。

场景有其生命周期

  • 创建 UIViewController 实例
  • 创建所有辅助对象
  • 将辅助对象保存到 UIViewController
  • 将所有对象连接成一个对象图。如果不存在 connect 闭环,此步骤将被跳过。
  • 当条件满足时,调用 awake() 方法于 UIViewController 及其所有辅助对象
  • 返回准备就绪的 UIViewcontroller

View 创建时发生以下顺序

  • 使用在 Instantiate() 方法中传入的闭包 view 创建 View,它将成为 UIViewController 的根视图。如果没有提供 view 闭包或 UIViewController 重写了 loadView() 方法,将使用 UIViewController 的标准机制来创建 view
  • 然后 UIViewController 及其辅助对象开始接收 UIViewController 生命周期事件。

支持重新创建 view

let viewController = Scene {/* creating scene */}
viewController.loadViewIfNeeded()
viewController.view = nil
viewController.loadViewIfNeeded()

执行此代码后,将重新创建 view 并与其建立所有连接。生命周期方法将按照 UIViewController 的逻辑到来。这有助于节省内存和其他资源。