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
})
网友评论