ViewModelBindable 1.0.1

ViewModelBindable 1.0.1

makesource 维护。



  • makesource

ViewModelBindable

⚡️绑定 ViewModel 的超级简单方法

  1. 将您想要注入的 ViewModel 类型定义为 alias
typealias ViewModel = DetailViewModel
  1. 注入 ViewModel
let viewController = DetailViewController()
let viewModel = DetailViewModel()
viewController.viewModel = viewModel
  1. 只需享受 bindViewModel(_:)bindStyles()
    这将自动被调用!

用法

只需将您的 UIView 或 UIViewController 转换为 ViewModelBindable 的即可!

ViewModelBindable 提供了两种绑定方法:bindViewModel(_:), bindStyles()

  • UIViewController
class DetailViewController: UIViewController {
  var disposeBag = DisposeBag()
  ...
}

extension DetailViewController: ViewModelBindable {
  typealias ViewModel = DetailViewModel
  
  // perform binding here
  func bindViewModel(viewModel: ViewModel) {
  }
  
  // This method is optional
  // Implement if you need
  func bindStyles() {
  }
}
  • UIView
class DetailView: UIView {
  var disposeBag = DisposeBag()
  ...
}

extension DetailView: ViewModelBindable {
  typealias ViewModel = DetailViewModel
  
  // perform binding here
  func bindViewModel(viewModel: ViewModel) {
  }
  
  // This method is optional
  // Implement if you need
  func bindStyles() {
  }
}

特色功能

ViewModelBindable 提供两种绑定方法,用于 MVVM 架构。

bindViewModel()

用法

bindViewModel() 用于将 View 和 ViewModel 进行绑定。

何时调用?

在 UIViewController 的 viewDidLoad() 之后立即

在 UIView 的 viewModel 注入之后立即

建议在 ViewController 的 bindViewModel(_:) 方法中绑定 UIView 的 viewModel

func bindViewModel(viewModel: ViewModel) {
  headerView.viewModel = viewModel
  footerView.viewModel = viewModel
}

如果使用 storyboard,你可以在 viewDidLoad / awakeFromNib 中初始化一些内容以准备绑定

bindStyles()

使用方法

bindStyles() 用于设置视图的样式。此方法不是必需的。因此,如果你需要,请实现它。

  • 如果你的应用程序支持本地化文本或图像,请在 'bindStyles()' 中设置样式
  • 如果你的应用程序支持基于用户设置的以主题为基础的设计
  • 如果你的应用程序不使用 Storyboard

何时调用?

在 UIViewController 初始 viewWillAppear(_:) 调用之前和每次 traitCollectionDidChange(_:) 调用之前

每次 traitCollectionDidChange(_:) 调用之前

依赖

要求

  • iOS 8.0+
  • Xcode 7.3+

安装

ViewModelBindable 通过 CocoaPods 提供。要安装它,只需在你的 Podfile 中添加以下行:

pod 'ViewModelBindable'

作者

makesource[email protected]

许可证

ViewModelBindable 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。