学习SwiftBond/Bond

github地址:https://github.com/SwiftBond/Bond

  • 监听textField输入框的值改变

textField.bnd_text
.observe { text in print(text) }


  • 将textField输入框的值改变,绑定到label的text上

textField.bnd_text
.bindTo(label.bnd_text)


  • 将textField输入框的值改变,绑定到label的text上,同时加个Hi在输入值前面

textField.bnd_text
.map { "Hi " + $0 }
.bindTo(label.bnd_text)


  • 监听button的TouchUpInside事件

button.bnd_controlEvent
.filter { $0 == UIControlEvents.TouchUpInside }
.observe { e in
print("Button tapped.")
}


  • 监听button的TouchUpInside事件的快捷方法

button.bnd_tap
.observe {
print("Button tapped.")
}


  • 合并监听,将email.length > 0 && pass.length > 0的结果绑定到button的bnd_enabled属性

combineLatest(emailField.bnd_text, passField.bnd_text)
.map { email, pass in
return email.length > 0 && pass.length > 0
}
.bindTo(button.bnd_enabled)


  • 很好的对MVVM的支持,监听viewModel的numberOfFollowers属性值改变,让后绑定到label.text

viewModel.numberOfFollowers
.map { "($0)" }
.bindTo(label.bnd_text)


  • 双向绑定viewModel和view,两边的值有改动,互相都会有改变

viewModel.username.bidirectionalBindTo(usernameTextField.bnd_text)


  • 监听通知的新写法

NSNotificationCenter.defaultCenter().bnd_notification("MyNotification")
.observe { notification in
print("Got (notification)")
}
.disposeIn(bnd_bag)


  • 给collectionView绑定数据源

repositories.bindTo(collectionView) { indexPath, array, collectionView in
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! RepositoryCell
let repository = array[indexPath.section][indexPath.item]
repository.name
.bindTo(cell.nameLabel.bnd_text)
.disposeIn(cell.onReuseBag)
repository.photo
.bindTo(cell.avatarImageView.bnd_image)
.disposeIn(cell.onReuseBag)
return cell
})

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容