Swift3 ReactiveSwift-更新可变属性的值?
作为收到的问题的后续,使用以下示例Swift3 ReactiveSwift-更新可变属性的值?,swift3,reactive-cocoa,Swift3,Reactive Cocoa,作为收到的问题的后续,使用以下示例 class Model { let mapType = MutableProperty<MKMapType>(.standard) } class SomeViewController: UIViewController { let viewModel: Model var mapView: MKMapView! init(viewModel: Model) { self.viewModel =
class Model {
let mapType = MutableProperty<MKMapType>(.standard)
}
class SomeViewController: UIViewController {
let viewModel: Model
var mapView: MKMapView!
init(viewModel: Model) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
mapView = // Create the map view
viewModel.mapType.producer.startWithValues { [weak self] mapType in
self?.mapView.mapType = mapType
}
// Rest of bindings
}
// The rest of the implementation...
}
class SomeOtherViewController: UIViewController {
let viewModel: Model
var segmentedControl: UISegmentedControl!
init(viewModel: Model) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
// Pretend we've setup the segmentedControl, and set its action to `updateMap(sender:)`...
// The rest of the initialization...
}
func updateMap(sender: UISegmentedControl) {
let newMapType =...
self.viewModel.mapType = newMapType // How do I update this value?
}
// The rest of the implementation...
}
类模型{
设mapType=MutableProperty(.standard)
}
类SomeViewController:UIViewController{
让视图模型:模型
var-mapView:MKMapView!
初始化(视图模型:模型){
self.viewModel=viewModel
super.init(nibName:nil,bundle:nil)
}
重写func viewDidLoad(){
super.viewDidLoad()
mapView=//创建地图视图
viewModel.mapType.producer.startWithValues{[weak self]中的映射类型
self?.mapView.mapType=mapType
}
//其余绑定
}
//其余的实现。。。
}
类SomeOtherViewController:UIViewController{
让视图模型:模型
var分段控制:UISegmentedControl!
初始化(视图模型:模型){
self.viewModel=viewModel
super.init(nibName:nil,bundle:nil)
//假设我们已经设置了segmentedControl,并将其操作设置为`updateMap(sender:)`。。。
//初始化的其余部分。。。
}
func updateMap(发送方:UISegmentedControl){
让newMapType=。。。
self.viewModel.mapType=newMapType//如何更新此值?
}
//其余的实现。。。
}
如何更新
Model.mapType
的值,并将其更改发送给任何观察者(如示例中的SomeViewController
)您只需设置其value
属性,这将触发更改自动发送给观察者
self.viewModel.mapType.value=newMapType
哇……我发誓我确实试过了。facepalm感谢您的快速回答!我也可以发誓这是例外的答案。。。。。修正了,再次感谢你!